嗨,我有这个是从教程中挑选出来的。链接在这里尝试帮助我理解这一点。
我想知道编码器如何尝试控制图像上传的大小。(我在其他地方看到过明确限制大小的代码,即
($_FILES["file"]["size"] < 20000)
)我问上述问题的原因是,当我上传较小的图像时效果很好,但较大的图像会发出一系列警告!
谢谢您的帮助。
代码
<?php
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$image =$_FILES["imagefile"]["name"];
$tempext = getExtension($image);
$extfile= mt_rand().".".$tempext;
$uploadedfile = $_FILES['imagefile']['tmp_name'];
if ($extfile)
{
$filename = stripslashes($extfile);
$extension = getExtension($filename); // return the type of image
$extension = strtolower($extension); // convert result to lowercase
if (($extension != "jpg") && ($extension != "jpeg")
&& ($extension != "png") && ($extension != "gif"))
{
$errors=1;
}
else
{
$size=filesize($_FILES['imagefile']['tmp_name']);
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['imagefile']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['imagefile']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0, $newwidth, $newheight, $width, $height);
$newwidth1=185;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp1,$src,0,0,0,0, $newwidth1, $newheight1, $width, $height);
$filename = "upload/". $extfile;
$filename1 = "upload/thumbs/". $extfile;
$etest1=imagejpeg($tmp,$filename,100);
$etest2=imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
?>