簡體   English   中英

需要幫助調整大小的圖像在PHP

[英]help needed to resize an image in php

我有一些代碼可以從上傳的圖像生成縮略圖。 唯一的問題是,它會剪切肖像圖像,而不是調整肖像圖像的大小以適合縮略圖的高度,而橫向圖像很好。 我想知道是否有人可以幫助我更改代碼,以便將肖像圖像正確放置在縮略圖中? (如果說得通?)

這是代碼:

$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創建圖像的方式,因此將不勝感激:)

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

我希望這有幫助!

希望這可以幫助。

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

adaptiveResizeImage

要么

adaptiveCropThumblanil

在Imagick可以幫助您

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM