PHP:如果超过 ..px 则调整大小为
如果图像尺寸超过 604x453,我想这样做,然后将其大小调整为 604x453。
我已经做到了这一点:
$org_name = stripslashes($_FILES[$upload_name]['name']);
$file_size = $_FILES[$upload_name]['size'];
$file_temp = $_FILES[$upload_name]['tmp_name'];
$file_type = $_FILES[$upload_name]["type"];
$file_err = $_FILES[$upload_name]['error'];
list($width, $height, $type, $attr) = $imageSizeInfo;
$move_me = "images/users/status/".$org_name;
if(move_uploaded_file($file_temp, $move_me)) {
echo "{";
echo "msg: '".$org_name."'";
echo "}";
}
if($width > 604 && $height > 453) {
$jpeg_quality = 90;
$src = "images/users/status/".$org_name;
$ext= pathinfo($src, PATHINFO_EXTENSION);
$targ_h = 453;
$targ_w = 604;
$path_thumbs = "images/users/status/";
$thumb_path = $path_thumbs . '/' . $newfilename;
if($ext == "jpg" OR $ext == "jpeg" OR $ext == "JPG"){
$img_r = imagecreatefromjpeg($src);
}elseif($ext == "png" OR $ext == "PNG"){
$img_r = imagecreatefrompng($src);
}elseif($ext == "gif" OR $ext == "GIF"){
$img_r = imagecreatefromgif($src);
}
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagejpeg($dst_r,$thumb_path,$jpeg_quality);
unlink($move_me);
}
所以首先它上传具有原始尺寸和所有内容的文件,然后检查尺寸,然后调整其大小,并取消链接(删除)原始文件..
现在我不知道是什么,但不知何故调整大小过程出错了,我只得到一个尺寸为 604x453 的黑色方块作为输出。
我错过了什么,我该如何解决这个问题?
I want to do so if the image are over the dimensions 604x453, then resize it to 604x453.
Ive made it this far:
$org_name = stripslashes($_FILES[$upload_name]['name']);
$file_size = $_FILES[$upload_name]['size'];
$file_temp = $_FILES[$upload_name]['tmp_name'];
$file_type = $_FILES[$upload_name]["type"];
$file_err = $_FILES[$upload_name]['error'];
list($width, $height, $type, $attr) = $imageSizeInfo;
$move_me = "images/users/status/".$org_name;
if(move_uploaded_file($file_temp, $move_me)) {
echo "{";
echo "msg: '".$org_name."'";
echo "}";
}
if($width > 604 && $height > 453) {
$jpeg_quality = 90;
$src = "images/users/status/".$org_name;
$ext= pathinfo($src, PATHINFO_EXTENSION);
$targ_h = 453;
$targ_w = 604;
$path_thumbs = "images/users/status/";
$thumb_path = $path_thumbs . '/' . $newfilename;
if($ext == "jpg" OR $ext == "jpeg" OR $ext == "JPG"){
$img_r = imagecreatefromjpeg($src);
}elseif($ext == "png" OR $ext == "PNG"){
$img_r = imagecreatefrompng($src);
}elseif($ext == "gif" OR $ext == "GIF"){
$img_r = imagecreatefromgif($src);
}
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagejpeg($dst_r,$thumb_path,$jpeg_quality);
unlink($move_me);
}
So first it uploads the file with the original dimensions and everything, and then after it checks for dimensions and then resizes it, and unlinks(remove) the original one..
Now I dont know what, but somehow at the resize procedure it goes wrong and i only get a black square in the dimensions 604x453 as output..
What did i miss, how can i solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
ImageCreateTrueColor
和imagejpeg
行之间,您需要如下内容:请参阅 PHP 手册以了解
imagecopyresampled()
。您的旧代码本质上是创建正确尺寸的空白画布,然后从中创建“黑色方块”。
您还应该使用
getimagesize()
来确定上传的图像类型,而不是使用文件扩展名。原因是上传的图像很可能名为
file.jpg
,但它实际上是一个.png
文件 -getimagesize( )
检查图像内的字节以确定其格式。Between your
ImageCreateTrueColor
andimagejpeg
lines, you need something like this:See the PHP manual for
imagecopyresampled()
.Your old code was essentially creating a blank canvas of the correct size, then creating your "black square" from it.
You should also use
getimagesize()
to determine what type of image has been uploaded, instead of using the file extension.The reason for this is that it is perfectly plausible that an image is uploaded which is called
file.jpg
, but it is actually a.png
file -getimagesize()
examines the bytes inside the image to determine what format it is.