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

发布于 2024-12-05 08:10:36 字数 850 浏览 0 评论 0原文

我有一些代码可以从上传的图像生成缩略图。唯一的问题是它会剪掉肖像图像,而不是调整肖像图像的大小以适应缩略图的高度,风景图像就很好。我想知道是否有人可以帮助我更改代码,以便它将肖像图像正确放置在缩略图内? (如果这是有道理的?)

这是代码:

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

I have a bit of code that generates thumbnails from an uploaded image. The only problem is that it cuts portrait images off, rather than resizing the portrait image to fit the height of the thumbnail, landscape images are fine. I was wondering if anyone could help me out to change the code so it will place portrait images inside the thumbnail properly? (if that makes sense?)

Here's the code:

$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);

I just don't fully understand the way php creates images, so any help would be appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕嗳→ 2024-12-12 08:10:36

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

我希望这会有所帮助!

The way you compute $newW and $newH is incorrect for what you want. You are giving preference to the width, so most landscape images will look okay, but portrait will be cut off. Try the following instead:

// 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 ) );
}

I hope this helps!

薄凉少年不暖心 2024-12-12 08:10:36

希望这有帮助。

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

//*************< /em>*********|根据高度调整大小

$photo_height = 350;

//************< em>**********|从帖子中获取文件

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

Hope this helps.

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

//**********************| Resize based on height

$photo_height = 350;

//**********************| Get the file from the post

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

//**********************| Check permission

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

//**********************| Make sure you have a file

 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];

//**********************| Make new name

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

//**********************| See the kind of file we have

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);
  }

//**********************| Get the height and width

  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";
}
你对谁都笑 2024-12-12 08:10:36

自适应调整图像大小

adaptiveCropThumblanil

可以帮助你

adaptiveResizeImage

or

adaptiveCropThumblanil

in Imagick can help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文