在php中调整图像大小所需的帮助

在php中调整图像大小所需的帮助,php,image,resize,thumbnails,Php,Image,Resize,Thumbnails,我有一些代码可以从上传的图像生成缩略图。唯一的问题是,它会切断纵向图像,而不是调整纵向图像的大小以适应缩略图的高度,横向图像是很好的。我想知道是否有人可以帮我修改代码,这样它就可以将肖像图像正确地放在缩略图中?(如果有道理的话?) 代码如下: $setSize = 150; $setHSize = 113; $jpeg_quality = 75; list($width, $height, $type, $attr) = getimagesize($origPat

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

代码如下:

$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创建图像的方式,所以如果有任何帮助,我将不胜感激:)

希望这有帮助

<?php 
define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);  
include_once(DOCROOT."/dbc.php");
//******************|从帖子中获取文件

$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");
}
//******************|改名

   $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";
}
希望这有帮助

<?php 
define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);  
include_once(DOCROOT."/dbc.php");
//******************|从帖子中获取文件

$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");
}
//******************|改名

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

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

我希望这有帮助

计算$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 ) );
}
我希望这有帮助

自适应视觉图像

自适应丙烯酰胺

在Imagick可以帮助你

自适应视觉图像

自适应丙烯酰胺


在Imagick中可以帮助您

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