【问题标题】:help needed to resize an image in php在 php 中调整图像大小所需的帮助
【发布时间】:2011-11-20 18:07:49
【问题描述】:

我有一些代码可以从上传的图像生成缩略图。唯一的问题是它会切断纵向图像,而不是调整纵向图像的大小以适应缩略图的高度,横向图像就可以了。我想知道是否有人可以帮助我更改代码,以便将肖像图像正确放置在缩略图中? (如果这有意义?)

代码如下:

$setSize        = 150;
$setHSize       = 113;
$jpeg_quality   = 75;

list($width, $height, $type, $attr) = getimagesize($origPath);

$newW = $setSize;
$newH = round( $height * ( $setSize / $width ) );

$img_r = imagecreatefromjpeg($origPath) or notfound();
$dst_r = ImageCreateTrueColor( $setSize, $setHSize );
$heightOffset = round( ($setHSize-$newH)/2 );

$white = imagecolorallocate($dst_r, 255, 255, 255);
imagefilledrectangle($dst_r, 0, 0, $setSize, $setHSize, $white);
imagecopyresampled($dst_r, $img_r, 0, $heightOffset, 0, 0, $newW, $newH, $width, $height);

header("Content-type: image/jpeg");
imagejpeg($dst_r, $thbPath, $jpeg_quality);

我只是不完全理解 php 创建图像的方式,所以任何帮助将不胜感激:)

【问题讨论】:

  • $setSize 和 $setHSize 是如何计算的?您的代码中缺少这一点,这就是问题所在。
  • 抱歉,它们是手动设置的,我已经编辑了代码以包含这些 :)

标签: php image resize thumbnails


【解决方案1】:

您计算 $newW 和 $newH 的方式不符合您的要求。您优先考虑宽度,因此大多数横向图像看起来都不错,但纵向图像会被截断。请尝试以下方法:

// assume landscape and see how things look
$newW = $setSize;
$newH = round( $height * ( $setSize / $width ) );
if ($newH > $setHSize) {
    // portrait image
    $newH = $setHSize;
    $newW = round( $width * ( $setHSize / $height ) );
}

我希望这会有所帮助!

【讨论】:

    【解决方案2】:

    希望这会有所帮助。

    <?php 
    define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);  
    include_once(DOCROOT."/dbc.php");
    

    //**********************|根据高度调整大小

    $photo_height = 350;
    

    //**********************|从帖子中获取文件

    $file = $_FILES['file'];
    $path_thumbs = (DOCROOT."/gallery/"); 
    $path_big = (DOCROOT."/trash/"); 
    

    //**********************|检查权限

    if (!is_writeable($path_thumbs)){
       die ("Error: The directory <b>($path_thumbs)</b> is NOT writable");
    }
    if (!is_writeable($path_big)){
        die ("Error: The directory <b>($path_big)</b> is NOT writable");
    }
    

    //**********************|确保你有一个文件

     if (isset($file)){
    
       $file_type = $_FILES['file']['type'];
       $file_name = $_FILES['file']['name'];
       $file_size = $_FILES['file']['size'];
       $file_tmp = $_FILES['file']['tmp_name'];
    
       $getExt = explode ('.', $file_name);
       $file_ext = $getExt[count($getExt)-1];
    

    //**********************|改名

       $rand_name = md5(time());
       $rand_name= rand(0,999999999);
    

    //**********************|查看我们拥有的文件类型

    if($file_size){
      if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
      $new_img = imagecreatefromjpeg($file_tmp);
      }
      elseif($file_type == "image/x-png" || $file_type == "image/png"){
      $new_img = imagecreatefrompng($file_tmp);
      }
      elseif($file_type == "image/gif"){
      $new_img = imagecreatefromgif($file_tmp);
      }
    

    //**********************|获取高度和宽度

      list($width, $height) = getimagesize($file_tmp);
           $imgratio=$height/$width;
    
     if ($photo_height >= $height){
     //*********** Dont resize if the image is smaller then $photo_height = 350;
     $newwidth = $width;
     $newheight = $height;
     }
     elseif ($imgratio>1){
     $newheight = $photo_height;
     $newwidth = $photo_height/$imgratio;
     }
     else{
     $newwidth = $photo_height;
     $newheight = $photo_height*$imgratio;
     }
    
     if (function_exists(imagecreatetruecolor)){ $resized_img = imagecreatetruecolor($newwidth,$newheight); }
    else{ die("Error: Please make sure you have GD library ver 2+");
    }
    
    imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    
    ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext",'100');
    ImageDestroy ($resized_img);
    ImageDestroy ($new_img);
    }
    move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
    $newimage = "$rand_name.$file_ext";
    }
    else { 
    echo "Sorry, there was a problem, Please try again.\n\n";
    }
    

    【讨论】:

      【解决方案3】:

      adaptiveResizeImage

      adaptiveCropThumblanil

      在 Imagick 中可以帮助你

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-16
        • 2014-08-07
        • 1970-01-01
        相关资源
        最近更新 更多