更改图像大小 - PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8978566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Image Size - PHP
提问by ZyteX
I have recently created an upload function, but I don't know how to change the width and height to 75px... I tried one code I found through Google, but I just got this error:
我最近创建了一个上传功能,但我不知道如何将宽度和高度更改为 75px...我尝试了通过 Google 找到的一个代码,但我刚刚收到此错误:
( ! ) Fatal error: Class 'Imagick' not found in C:\wamp\www\Legendary\new\usersettings.php on line 725
Call Stack
# Time Memory Function Location
1 0.0042 880616 {main}( ) ..\usersettings.php:0
Here's my current code (including the code which didn't work):
这是我当前的代码(包括不起作用的代码):
echo '
<table border="0" width="100%">
<tr><td style="font-size: 16px;">Change Image</td></tr>
<form action="" method="post" enctype="multipart/form-data">
<tr><td>Upload Image:</td><td style="text-align: right;"><input type="file" name="upimage" id="upimage" /></td></tr>
<tr><td></td><td style="text-align: right; font-size: 10px;"></td></tr>
<tr><td></td><td style="text-align: right;"><input type="submit" name="submitnewimage" value="Upload" class="button" /></td></tr>
</form>
';
echo '
</table>
';
if(isset($_POST['submitnewimage'])){
$name = $_FILES['upimage']['name'];
$temp = $_FILES['upimage']['tmp_name'];
$type = $_FILES['upimage']['type'];
$size = $_FILES['upimage']['size'];
if($name!=""){
include 'config.php';
$sql5 = mysql_query("SELECT * FROM images ORDER BY id DESC LIMIT 1");
while($row=mysql_fetch_array($sql5)) {
if(!isset($show2)){
$id = $row['id'];
$id = $id + 1;
$show2 = "YES";
}
}
if(($type=="image/jpeg") || ($type=="image/jpg") || ($type=="image/gif") || ($type=="image/pjpeg") || ($type=="image/png")){
if($size<=100000){
$pos = strrpos($name, '.');
if($pos === false)
$ext = "";
$ext = substr($name, $pos);
$newFilename = $id.$ext;
move_uploaded_file($temp, "images/teamicons/".$newFilename);
$im = new Imagick('images/teamicons/'.$newFilename);
$im->thumbnailImage(75,75);
$im->writeImage('images/teamicons/'.$newFilename);
mysql_query("INSERT INTO `images`(`id`, `name`, `size`, `type`) VALUES (NULL,'$newFilename',$size,'$type')");
$myusername = $_SESSION['myusername'];
mysql_query("UPDATE `members` SET `img`= '$newFilename' WHERE `username`='$myusername'");
header("Location:" . $_SESSION['prev_page']);
}else{echo "<tr><td colspan='2'><span style='color:#F00;'>The file, "".$name."", is too large! Maximum allowed file size is 100kB.</span></td></tr>";}
}else{echo "<tr><td colspan='2'><span style='color:#F00;'>"".$type."" is not a valid file type!</span></td></tr>";}
}else{echo "<tr><td colspan='2'><span style='color:#F00;'>No file has been specified!</span></td></tr>";}
}
Is there any way of changing the width and height of the images?
有没有办法改变图像的宽度和高度?
回答by Max Spencer
I've successfully used GDto do this recently, specifically using the imagecopyresampled
function.
我最近成功地使用GD来做到这一点,特别是使用该imagecopyresampled
功能。
To expand a little on that... Once I had the image uploaded (which I wont go into, because that is a whole other issue), I did something fairly simple like this:
稍微扩展一下......一旦我上传了图像(我不会进入,因为这是另一个问题),我做了一些非常简单的事情:
$original_info = getimagesize($filename);
$original_w = $original_info[0];
$original_h = $original_info[1];
$original_img = imagecreatefromjpg($filename);
$thumb_w = 100;
$thumb_h = 100;
$thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
imagecopyresampled($thumb_img, $original_img,
0, 0,
0, 0,
$thumb_w, $thumb_h,
$original_w, $original_h);
imagejpeg($thumb_img, $thumb_filename);
imagedestroy($thumb_img);
imagedestroy($original_img);
Please note that I have not tested this code. It's just here to give you a basic idea of my method.
请注意,我尚未测试此代码。在这里只是为了让您对我的方法有一个基本的了解。
回答by dimitril
The Imagick Class is not found, because it is a PHP extension you need to install on your server.
找不到 Imagick 类,因为它是您需要在服务器上安装的 PHP 扩展。
Read the following documentation to find instructions on how to use/install the extension. http://www.php.net/manual/en/book.imagick.php
阅读以下文档以查找有关如何使用/安装扩展的说明。 http://www.php.net/manual/en/book.imagick.php
回答by TMS
I use code like this:
我使用这样的代码:
$t = imagecreatefromjpeg($old_path);
$x = imagesx($t);
$y = imagesy($t);
$s = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($s, $t, 0, 0, 0, 0, $new_width, $new_height,
$x, $y);
imagejpeg($s, $new_path);
chmod($new_path, 0644);
回答by TMS
You can also scale the image client-side, e.g. using plupload library(can resize JPEG and PNG).
您还可以在客户端缩放图像,例如使用plupload 库(可以调整 JPEG 和 PNG 的大小)。
回答by Vasilii Suricov
Simple solutions some times changed alfa (transparent png). For me forks only this (it's real, just copy-pasted code)
简单的解决方案有时会改变 alfa(透明 png)。对我来说只有这个(它是真实的,只是复制粘贴的代码)
public static function resize($filename, $maxW, $maxH, $ext = null)
{
if ($ext === null)
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
else
$ext = strtolower($ext);
list($origW, $origH) = getimagesize($filename);
$w = $origW;
$h = $origH;
# taller
if ($h > $maxH) {
$w = ($maxH / $h) * $w;
$h = $maxH;
}
# wider
if ($w > $maxW) {
$h = ($maxW / $w) * $h;
$w = $maxW;
}
$resource = imagecreatetruecolor($w, $h);
# MUST IMPORTANT TWO ROWS
imagesavealpha($resource, true);
imagefill($resource, 0, 0, 0x7fffffff);
if ($ext == 'jpeg' || $ext == 'jpg') {
$image = imagecreatefromjpeg($filename);
} else if ($ext == 'png') {
$image = imagecreatefrompng($filename);
} else {
throw new Exception('Unsupported extension');
}
imagecopyresampled(
$resource,
$image, 0, 0, 0, 0,
$w, $h, $origW, $origH
);
imagedestroy($image);
return $resource;
}