【问题标题】:PHP Image Resize, with Thumbnailer IssuePHP图像调整大小,缩略图问题
【发布时间】:2014-03-03 18:51:22
【问题描述】:

图像大小调整功能存在问题。

功能

// Image Resizer
function MakeThumbnail($inputFile, $filepath, $ext, $maxWidth, $maxHeight){
    /* Get some details about the image */
    $srcDetails = getimagesize($inputFile);
    switch ($srcDetails[2]) {
        case 1: //GIF
            $source_image = imagecreatefromgif($inputFile);
            break;          
        case 2: //JPEG
            $source_image = imagecreatefromjpeg($inputFile);
            break;          
        case 3: //PNG
            $source_image = imagecreatefrompng($inputFile);
            break;          
        case 6: //WBMP
            $source_image = imagecreatefromwbmp($inputFile);
            break;          
        default:
            break;
    }       
    /* Original Dimensions */
    $width = imagesx($source_image);
    $height = imagesy($source_image);       
    /* find the "desired height" of this thumbnail, relative to the desired width, and vice-versa */
    if(($maxWidth <= $width) && $maxHeight <= $height){
        if($target_ratio == $original_ratio){
            $desired_height = $maxHeight;
            $desired_width = $maxWidth;
        }elseif($target_ratio > $original_ratio){
            $desired_height = $maxHeight;
            $desired_width = $width * ($maxHeight / $height);
        }elseif($target_ratio < $original_ratio){
            $desired_height = $height * ($maxWidth / $width);
            $desired_width = $maxWidth;
        }
    }else{
        $desired_height = $maxHeight;
        $desired_width = $maxWidth;
    }
imagecopyresized($virtual_image,$source_image,0,0,0,0,$maxWidth,$desired_height,$width,$height);        
    /* create the physical thumbnail image to its destination */
    switch ($srcDetails[2]) {
        case 1: //GIF
            imagegif($virtual_image, $filepath);
            imagedestroy($virtual_image);
            break;          
        case 2: //JPEG
            imagejpeg($virtual_image, $filepath, 100);
            imagedestroy($virtual_image);
            break;          
        case 3: //PNG
            imagepng($virtual_image, $filepath, 6);
            imagedestroy($virtual_image);
            break;          
        case 6: //WBMP
            imagewbmp($virtual_image, $filepath);
            imagedestroy($virtual_image);
            break;          
        default:
            imagedestroy($virtual_image);
            break;          
    }
}

用法

MakeThumbnail($pic2['tmp_name'], $pic2Path, $extension, 800, 600); //<- resize original
MakeThumbnail($pic2['tmp_name'], $pic2ThumbPath, $extension, 150, 100); //<- make a thumbnail

使用比例调整图像大小现在可以与上面的更新功能一起使用。我现在发现的问题是,如果原始宽度小于我要指定的宽度,图像会被调整大小(变大)。

我追求的是一个调整大小的图像,它永远不会比$maxWidth 宽,永远不会高于$maxHeight,但要保持原始图像的宽高比。

【问题讨论】:

  • 你想在这里做什么? $newwidth = $newheight = min($size, max($width, $height));
  • 我的意思是你在用我引用的那行代码尝试什么;我认为这是你麻烦的根源。
  • 我在上一个问题中找到了您在此处继承的原始代码:stackoverflow.com/questions/20707086/…。它已经在保持纵横比的同时调整大小,只需将 MaxHeight 值设置为 0。
  • 你是什么意思“不,对不起它没有?”我在看代码,如果你把 0 作为 maxHeight,它会根据纵横比计算高度。它就在这里$newheight = (strlen($maxHeight)&gt;0) ? $maxHeight : ($height / $width) * $newwidth;

标签: php image image-processing gd


【解决方案1】:

我想你想要这个:

    $width = imagesx($source_image);
    $height = imagesy($source_image);     
    // new code to check if image is too small   
    if ($width <= $maxWidth || height <= $maxHeight) { //because we need to check either, and not both
     // image is too small; don't resize
        $doResize = false;
    } else {
        $doResize = true;
    }

然后围绕你的 imagecopyresized 放:

if ($doResize) {
     imagecopyresized($virtual_image,$source_image,0,0,0,0,$maxWidth,$desired_height,$width,$height);        
}

【讨论】:

  • 我想你可以看到我的代码在尝试什么,对吧?检查错误并调整。可以是 var 大小写吗?
【解决方案2】:

知道了。

    /* Get some details about the image */
    $srcDetails = getimagesize($inputFile);
    switch ($srcDetails[2]) {
        case 1: //GIF
            $source_image = imagecreatefromgif($inputFile);
            break;          
        case 2: //JPEG
            $source_image = imagecreatefromjpeg($inputFile);
            break;          
        case 3: //PNG
            $source_image = imagecreatefrompng($inputFile);
            break;          
        case 6: //WBMP
            $source_image = imagecreatefromwbmp($inputFile);
            break;          
        default:
            break;
    }       
    /* Original Dimensions */
    $width = imagesx($source_image);
    $height = imagesy($source_image);   
    $target_ratio = $maxWidth / $maxHeight;
    $original_ratio = $width / $height; 
    /* find the "desired height" of this thumbnail, relative to the desired width, and vice-versa */
    if(($maxWidth <= $width) || $maxHeight <= $height){ // <-- needed to change this to orelse
        if($target_ratio == $original_ratio){
            $desired_height = $maxHeight;
            $desired_width = $maxWidth;
        }elseif($target_ratio > $original_ratio){
            $desired_height = $maxHeight;
            $desired_width = $width * ($maxHeight / $height);
        }elseif($target_ratio < $original_ratio){
            $desired_height = $height * ($maxWidth / $width);
            $desired_width = $maxWidth;
        }
    }else{
        $desired_height = $maxHeight;
        $desired_width = $maxWidth;
    }
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);     
    imagecopyresized($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);      
    /* create the physical thumbnail image to its destination */
    switch ($srcDetails[2]) {
        case 1: //GIF
            imagegif($virtual_image, $filepath);
            imagedestroy($virtual_image);
            break;          
        case 2: //JPEG
            imagejpeg($virtual_image, $filepath, 100);
            imagedestroy($virtual_image);
            break;          
        case 3: //PNG
            imagepng($virtual_image, $filepath, 6);
            imagedestroy($virtual_image);
            break;          
        case 6: //WBMP
            imagewbmp($virtual_image, $filepath);
            imagedestroy($virtual_image);
            break;          
        default:
            imagedestroy($virtual_image);
            break;          
    }

【讨论】:

    猜你喜欢
    • 2011-06-03
    • 2012-01-06
    • 2011-10-22
    • 2011-09-03
    • 2011-09-15
    • 2013-06-22
    • 2013-06-24
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多