PHP:如果超过 ..px 调整为 [英] PHP: if more than ..px resize to

查看:58
本文介绍了PHP:如果超过 ..px 调整为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果图像尺寸超过 604x453,我想这样做,然后将其调整为 604x453.

I want to do so if the image are over the dimensions 604x453, then resize it to 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);
}

因此,它首先上传包含原始尺寸和所有内容的文件,然后检查尺寸并调整其大小,然后取消链接(删除)原始尺寸..

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..

现在我不知道是什么,但不知何故在调整大小的过程中它出错了,我只得到一个尺寸为 604x453 的黑色方块作为输出..

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?

推荐答案

在你的 ImageCreateTrueColorimagejpeg 行之间,你需要这样的东西:

Between your ImageCreateTrueColor and imagejpeg lines, you need something like this:

imagecopyresampled($dst_r, $img_r, 0, 0, 0, 0, $targ_w, $targ_h, $width, $height);

请参阅 PHP 手册以了解 imagecopyresampled().

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.

您还应该使用 getimagesize() 确定上传的是什么类型的图片,而不是使用文件扩展名.

You should also use getimagesize() to determine what type of image has been uploaded, instead of using the file extension.

索引 2 是 IMAGETYPE_XXX 之一指示类型的常量图像.

Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.

这样做的原因是上传了一个名为 file.jpg 的图像是完全合理的,但它实际上是一个 .png 文件 - getimagesize() 检查图像中的字节以确定它是什么格式.

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.

这篇关于PHP:如果超过 ..px 调整为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆