PHP:如果超过 ..px 则调整大小为

发布于 2024-10-10 18:42:14 字数 1368 浏览 0 评论 0原文

如果图像尺寸超过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2024-10-17 18:42:14

ImageCreateTrueColorimagejpeg 行之间,您需要如下内容:

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

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

您的旧代码本质上是创建正确尺寸的空白画布,然后从中创建“黑色方块”。

您还应该使用 getimagesize() 来确定上传的图像类型,而不是使用文件扩展名。

索引 2 是 IMAGETYPE_XXX 之一
指示类型的常量
图片。

原因是上传的图像很可能名为 file.jpg,但它实际上是一个 .png 文件 - getimagesize( ) 检查图像内的字节以确定其格式。

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);

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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文