此脚本用于缩小图像大小。它仅适用于尺寸小于2MB的图像。当我使用大于此尺寸的图片时,它会在getimagesize
中生成错误,错误为getimagesize(): Filename cannot be empty
。我不知道为什么它不起作用。我已将内存限制更改为1024 MB,但仍无效。
注意:我使用wamp服务器
<?PHP
function imagecrop($img_name,$newname,$type,$modwidth,$modheight)
{
list($width, $height) = getimagesize($img_name) ; //get width & height in array list
$tn = imagecreatetruecolor($modwidth, $modheight);
if(!strcmp("image/png",$type))
{
imagealphablending($tn, false); //For transparent BackGround
imagesavealpha($tn, true);
}
if(!strcmp("image/jpg",$type) || !strcmp("image/jpeg",$type) || !strcmp("image/pjpeg",$type))
$src_img=imagecreatefromjpeg($img_name);
if(!strcmp("image/png",$type))
$src_img=imagecreatefrompng($img_name);
if(!strcmp("image/gif",$type))
$src_img=imagecreatefromgif($img_name);
imagecopyresampled($tn, $src_img, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
if(!strcmp("image/png",$type))
{
imagesavealpha($src_img, true);
$ok=imagepng($tn,$newname);
}
else if(!strcmp("image/gif",$type))
{
$ok=imagegif($tn,$newname);
}
else
{
$ok=imagejpeg($tn,$newname);
}
if($ok==1)
{
return "<img src=".$_FILES['image']['name']." border='0'>";
}
}
if(isset($_POST['Resize']))
{
// imagecrop(Upload Image tmp_path,Upload Image store path,Upload Image type,resize width,resize height);
echo imagecrop($_FILES['image']['tmp_name'],$_FILES['image']['name'],$_FILES['image']['type'],650,250);
}
?>
HTML:
<form enctype="multipart/form-data" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10%">Upload Image : </td>
<td width="24%">
<input type="file" name="image" /> </td>
<td width="66%"><input type="submit" name="Resize" value="Submit" /></td>
</tr>
</table>
</form>