Php 如何使用GD库将照片上载调整为精确尺寸

Php 如何使用GD库将照片上载调整为精确尺寸,php,gd,Php,Gd,我已经学会了如何在上传到带有GD库的服务器后调整图像的大小。它的作品完美,但我需要的图像大小调整到180 x 150像素。根据图像的原始大小,它有时会小于150或小于180。如何调整图像的大小,使其每次精确到180像素x 150像素 PHP上传脚本 // Access the $_FILES global variable for this specific file being uploaded // and create local PHP variables from the $_FILE

我已经学会了如何在上传到带有GD库的服务器后调整图像的大小。它的作品完美,但我需要的图像大小调整到180 x 150像素。根据图像的原始大小,它有时会小于150或小于180。如何调整图像的大小,使其每次精确到180像素x 150像素

PHP上传脚本

// Access the $_FILES global variable for this specific file being uploaded
// and create local PHP variables from the $_FILES array of information
$fileName = $_FILES["uploaded_file"]["name"]; // The file name
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
$fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true
$kaboom = explode(".", $fileName); // Split file name into an array using the dot
$fileExt = end($kaboom); // Now target the last array element to get the file extension
// START PHP Image Upload Error Handling --------------------------------------------------
if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
echo "ERROR: Your file was larger than 5 Megabytes in size.";
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
exit();
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName) ) {
 // This condition is only if you wish to allow uploading of specific file types    
 echo "ERROR: Your image was not .gif, .jpg, or .png.";
 unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
 exit();
} else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
echo "ERROR: An error occured while processing the file. Try again.";
exit();
}
// END PHP Image Upload Error Handling ----------------------------------------------------
// Place it into your "uploads" folder mow using the move_uploaded_file() function
$moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");
// Check to make sure the move result is true before continuing
if ($moveResult != true) {
echo "ERROR: File not uploaded. Try again.";
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
exit();
}
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
// ---------- Include Universal Image Resizing Function --------
include_once("ak_php_img_lib_1.0.php");
$target_file = "uploads/$fileName";
$resized_file = "uploads/resized_$fileName";
$wmax = 180;
$hmax = 151;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
// ----------- End Universal Image Resizing Function -----------
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
echo "The file extension is <strong>$fileExt</strong><br /><br />";
echo "The Error Message output for this upload is: $fileErrorMsg";
?>
//访问正在上载的特定文件的$\u FILES全局变量
//并从$\u文件数组中创建本地PHP变量
$fileName=$\u文件[“上载的\u文件”][“名称”];//文件名
$fileTmpLoc=$\u文件[“上载的\u文件”][“tmp\u名称”];//PHP tmp文件夹中的文件
$fileType=$\u文件[“上载的\u文件”][“类型”];//它所属的文件类型
$fileSize=$\u文件[“上载的\u文件”][“大小”];//文件大小(字节)
$fileErrorMsg=$\u文件[“上载的\u文件”][“错误”];//0表示错误。。。1为真
$kaboom=explode(“.”,$fileName);//使用点将文件名拆分为数组
$fileExt=end($kaboom);//现在以最后一个数组元素为目标来获取文件扩展名
//启动PHP图像上载错误处理--------------------------------------------------
如果(!$fileTmpLoc){//如果未选择文件
echo“错误:请在单击上载按钮之前浏览文件。”;
退出();
}else if($fileSize>5242880){//如果文件大小大于5 MB
echo“错误:您的文件大小超过5 MB。”;
取消链接($fileTmpLoc);//从PHP临时文件夹中删除上载的文件
退出();
}如果(!preg_match(“/(gif | jpg | png)$/i“,$fileName)){
//此条件仅在您希望允许上载特定文件类型时适用
echo“错误:您的图像不是.gif、.jpg或.png。”;
取消链接($fileTmpLoc);//从PHP临时文件夹中删除上载的文件
退出();
}else if($fileErrorMsg==1){//如果文件上载错误键等于1
echo“错误:处理文件时出错。请重试。”;
退出();
}
//结束PHP图像上载错误处理----------------------------------------------------
//使用move_upload_file()函数将其放入“uploads”文件夹mow中
$moveResult=move_uploads_file($fileTmpLoc,“uploads/$fileName”);
//继续之前,请检查以确保移动结果为真
如果($moveResult!=true){
echo“错误:文件未上载。请重试。”;
取消链接($fileTmpLoc);//从PHP临时文件夹中删除上载的文件
退出();
}
取消链接($fileTmpLoc);//从PHP临时文件夹中删除上载的文件
//------包括通用图像大小调整功能--------
包括一次(“ak_php_img_lib_1.0.php”);
$target_file=“uploads/$fileName”;
$resized\u file=“上传/调整大小的\u$fileName”;
$wmax=180;
$hmax=151;
ak_img_resize($target_file,$resized_file,$wmax,$hmax,$fileExt);
//------结束通用图像大小调整功能-----------
//在页面上显示内容,以便您可以查看正在进行的测试
echo“名为$fileName的文件已成功上载。

”; echo“大小为$fileSize字节。

”; echo“它是一种$fileType类型的文件。

”; echo“文件扩展名为$fileExt

”; echo“此上载的错误消息输出为:$fileErrorMsg”; ?>
PHP调整大小脚本

<?php
// Function for resizing jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
       $w = $h * $scale_ratio;
} else {
       $h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){ 
  $img = imagecreatefromgif($target);
} else if($ext =="png"){ 
  $img = imagecreatefrompng($target);
} else { 
  $img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>

您正在使用的函数,
ak_img_resize
明确缩放图像(以免产生扭曲的结果):

要始终将大小精确调整为180x150,请将上述行替换为:

$w = 180;
$h = 150;
请注意,这将产生扭曲的结果,除非源图像具有完全相同的宽高比(1.2)

如果你想要一个不失真的结果,那么你需要调整大小和过大的高度或宽度。

你可以考虑这个问题。还有,看看这个。如果你能使用imagick,我建议你使用。GD库的一个大缺点(有几个)是您无法以本机方式调整动画GIF的大小,并且需要或其他一些单独的库。
$w = 180;
$h = 150;