1
我想調整服務器端的圖像大小。我正在獲取圖像,但是當我嘗試調整圖像大小時;這是拋出一些錯誤。這是我所做的:如何解決這個圖像在PHP中調整大小錯誤?初學者
imgresize.php:此文件創建調整大小的圖像。
<?php
function compressImage($ext, $uploadedfile, $actual_image_name, $newwidth)
{
if($ext=="jpg" || $ext=="jpeg"){
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// $filename = $newwidth.'_'.$actual_image_name; //PixelSize_TimeStamp.jpg
$filename = $actual_image_name; //PixelSize_TimeStamp.jpg
imagejpeg($tmp,$filename,100);
imagedestroy($tmp);
}
else if($ext=="png"){
$src = imagecreatefrompng($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// $filename = $newwidth.'_'.$actual_image_name; //PixelSize_TimeStamp.jpg
$filename = $actual_image_name; //PixelSize_TimeStamp.jpg
imagepng($tmp,$filename,100);
imagedestroy($tmp);
}
else{
}
return $filename;
}
?>
這裏是我的代碼得到它在我的服務器端:
include 'imgresize.php';
if($_FILES['img']['tmp_name'] != ''){
$tmp_name_array = $_FILES['img']['tmp_name'];
$uploadedImageName = $_FILES['img']['name'][0];
$imageName = htmlspecialchars($_FILES['img']['tmp_name']);
$imgSize = $_FILES['img']['size'];
//$name= htmlspecialchars($_FILES['img']['name']);
print_r($_FILES['img']);
$tempImgName = $_FILES['img']['tmp_name'];
if(strlen($tempImgName)){
$ext = pathinfo($tempImgName, PATHINFO_EXTENSION);
if(in_array($ext, $valid_formats)){
if($imgSize < 2*1024*1024){
$imageName = compressImage($ext, $tempImgName, $tempImgName, 200);
$imageContents = file_get_contents($imageName);
$encodedImage = base64_encode($imageContents);
array_push($uploadImage, $encodedImage);
// database query
} else{
echo "<script type=\"text/javascript\">".
"window.alert('Image size should be less than 2MB.');".
"window.location.href='edit-profile.php?id=".$id."';".
"</script>";
}
} else{
echo "<script type=\"text/javascript\">".
"window.alert('Invalid image extension found. (Image should be of jpg, jpeg, png extension only) ');".
"window.location.href='edit-profile.php?id=".$id."';".
"</script>";
}
}
它給了我下面的錯誤:
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: imagecreatefromjpeg(11150825_10153266879053764_3215821412576026934_n.jpg): failed to open stream: No such file or directory in /home/user/public_html/utils/imgresize.php on line 6
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: getimagesize(11150825_10153266879053764_3215821412576026934_n.jpg): failed to open stream: No such file or directory in /home/user/public_html/utils/imgresize.php on line 15
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: Division by zero in /home/user/public_html/utils/imgresize.php on line 17
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: imagecreatetruecolor(): Invalid image dimensions in /home/prernnys/public_html/utils/imgresize.php on line 19
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /home/prernnys/user/utils/imgresize.php on line 20
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /home/user/public_html/utils/imgresize.php on line 25
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: imagedestroy() expects parameter 1 to be resource, boolean given in /home/user/public_html/utils/imgresize.php on line 27
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: file_get_contents(11150825_10153266879053764_3215821412576026934_n.jpg): failed to open stream: No such file or directory in /home/user/public_html/update.php on line 916
的圖像是沒有得到調整預期。請糾正我錯誤的地方。
是的。你是對的桑尼。但是,我使用file_get_contents($ _ file ['img'])獲取文件內容。我如何進一步調整它的大小?我打算使用TinyPng api,但後來認爲我自己做了。因此,上述情況。 – Nevermore
我認爲你在這裏有2個選項。首先是先保存文件,調整圖像大小並覆蓋文件。第二種是使用在服務器端生成的臨時文件,調整大小並將圖像保存在所需的路徑中。使用第二個選項,您需要知道臨時文件首先存儲在服務器端的位置。看看php文檔http://php.net/manual/en/function.sys-get-temp-dir.php – andreasonny83
當我嘗試使用服務器端生成的臨時文件時,它沒有擴展,所以圖像永遠不會調整大小。 – Nevermore