doukao8851 2011-01-09 15:26
浏览 32
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • dsfw2154 2011-01-09 15:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题