帮助需要在PHP中调整图像大小

时间:2011-09-20 04:03:56

标签: php image resize thumbnails

我有一些代码可以从上传的图片中生成缩略图。唯一的问题是它会关闭肖像图像,而不是调整肖像图像的大小以适应缩略图的高度,景观图像很好。我想知道是否有人可以帮助我改变代码,以便将肖像图像正确放入缩略图内? (如果这有意义吗?)

以下是代码:

$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创建图像的方式,所以任何帮助都会受到赞赏:)

3 个答案:

答案 0 :(得分:3)

您计算$ 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 ) );
}

我希望这有帮助!

答案 1 :(得分:1)

希望这有帮助。

<?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";
}

答案 2 :(得分:0)

  

adaptiveResizeImage

  

adaptiveCropThumblanil

Imagick中的

可以帮助你