我想上传一张图片,并有一个缩略图版本,最大宽度为150像素,最大高度为400像素,并保存为thumbnail-filename
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$tempext = explode('.',$_FILES['image']['name']);
$file_ext=strtolower(end($tempext));
$extensions = array("jpeg","jpg","png","gif","");
if(in_array($file_ext,$extensions )=== false){
$errors[]="extension not allowed, please choose a different file.";
}
if($file_size > 419430400){
$errors[]='Maximum file size is 400mb';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../large_format/a0/".$file_name);
echo '<script>function myFunction(){alert("Success!");} myFunction();</script>';
}else{
print_r($errors);
}
所以会有一个大图像文件和它的缩略图版本。 我真的很感激建议代码的例子。
提前致谢
答案 0 :(得分:0)
尝试使用getimagesize()
函数Link。
//upload orginal size photo
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);
if ($width > 150){
$newwidth=150;
$newheight=($height/$width)*600;
}else{
$newwidth=$width;
}
if($height>400)
{
$newheight=400
}
else {
$newheight=$height;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
答案 1 :(得分:0)
试试此代码
您无需更改这些功能
function resizeImage($image,$width,$height,$scale) {
list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType);
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
switch($imageType) {
case "image/gif":$source=imagecreatefromgif($image); break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":$source=imagecreatefromjpeg($image);break;
case "image/png":
case "image/x-png":$source=imagecreatefrompng($image); break;
}
$size_fit=$_SESSION['size_fit'];
if($size_fit==1){
if ($width > 140){
$newwidth=140;
$newheight=($height/$width)*140;
}else {$newwidth=$width;
$newheight=$height;}
imagecopyresampled($newImage,$source,0,0,0,0,$newwidth,$newheight,$width,$height);
}else{
imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
}
switch($imageType) {
case "image/gif":imagegif($newImage,$image); break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":imagejpeg($newImage,$image,90);break;
case "image/png":
case "image/x-png":imagepng($newImage,$image); break;
}
chmod($image, 0777);
return $image;
}