在下面的代码中,我尝试在上传后调整图像大小。使用echo语句完成的所有检查都会获得正确的数据,但不会调整大小并将其放在文件夹中。
<?php
if (!$error) {
// location where inital upload will be moved to
$name = $_FILES['uploaded']['name'];
$tname = $_FILES['uploaded']['tmp_name'];
$folder = "uploads/" ;
$dest_folder = "gallery/";
// find the type of image
switch ($_FILES["uploaded"]["type"]) {
case $_FILES["uploaded"]["type"] == "image/gif":
move_uploaded_file($_FILES["uploaded"]["tmp_name"],$folder.$name);
break;
case $_FILES["uploaded"]["type"] == "image/jpeg":
move_uploaded_file($_FILES["uploaded"]["tmp_name"],$folder.$name);
break;
case $_FILES["uploaded"]["type"] == "image/pjpeg":
move_uploaded_file($_FILES["uploaded"]["tmp_name"],$folder.$name);
break;
case $_FILES["uploaded"]["type"] == "image/png":
move_uploaded_file($_FILES["uploaded"]["tmp_name"],$folder.$name);
break;
case $_FILES["uploaded"]["type"] == "image/x-png":
move_uploaded_file($_FILES["uploaded"]["tmp_name"],$folder.$name);
break;
default:
$error[] = 'Wrong image type selected. Only JPG, PNG or GIF accepted!.';
}///END OF SWITCH////
$resize = TRUE;
}//END IF NO ERRORS///
if ($resize == TRUE) {
//echo "$folder<br />";
//$file_name = $folder;
$file_name = $name;
$src_folder = $folder;
thumb($file_name,$folder,$dest_folder,$tname);
}
?>
这是我正在调用的函数:
function thumb ($file_name,$folder,$dest_folder,$tname) {
list($src_width, $src_height, $imagetype) = getimagesize($folder.$file_name);
$imagetype = image_type_to_mime_type($imagetype);
$org_w = 500;
//$dest_folder = "uploads/gallery/";
//$new_img = $dest_folder;
echo "Original width is $src_width<br />";
echo "Orginal height is $src_height<br />";
echo "Image type is $imagetype<br />";
echo "Name of file is $file_name<br />";
echo "tmp_name is $tname<br />";
echo "New folder is $dest_folder<br />";
switch( $imagetype ) {
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$source = imagecreatefromjpeg($tname);
echo "image is a jpg.<br />";
break;
case "image/png":
$source = imagecreatefrompng($tname);
break;
case "image/gif":
$source = imagecreatefromgif($tname);
echo "image is a gif.";
break;
default:
return false;
}
$org_h = ($src_height/$src_width)* $org_w;
echo "h=$org_h<br />";
echo "w=$org_w<br />";
$tmp =imagecreatetruecolor($orig_w,$org_h);
imagecopyresampled($tmp,$source,0,0,0,0,$orig_w,$org_h,$src_width,$src_height);
switch($imagetype) {
case "image/gif":
imagegif($tmp, $dest_folder.$file_name,80);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
imagejpeg($tmp, $dest_folder.$file_name,80);
break;
case "image/png":
case "image/x-png":
imagepng($tmp, $dest_folder.$file_name,80);
break;
}
}
为什么不调整它并把它放在文件夹中? STUCK !!!
答案 0 :(得分:1)
好的,首先,你的问题......
如果有一个有效的mime类型,您的脚本会将上传的文件移动到
'uploads/' . $name
因此,$tname
不再是有效路径,但您仍将其传递给thumb()
函数,用于使用GD createfrom*
函数之一创建图像资源。 / p>
这显然不起作用,因为$tname
没有图片。
其他问题......
您的第一个开关块可以缩小为
switch ($_FILES["uploaded"]["type"]) {
case "image/gif":
case "image/jpeg":
case "image/pjpeg":
case "image/png":
case "image/x-png":
move_uploaded_file($_FILES["uploaded"]["tmp_name"],$folder.$name);
break;
default:
$error[] = 'Wrong image type selected. Only JPG, PNG or GIF accepted!.';
}
在这一行
if($ resize == TRUE)
很有可能$resize
未定义。您至少应该将其初始化为顶部的false
答案 1 :(得分:0)
一些问题,但这是阻止拇指功能正常工作的原因:
更改
$org_h = ($src_height/$src_width)* $org_w;
echo "h=$org_h<br />";
echo "w=$org_w<br />";
$tmp =imagecreatetruecolor($orig_w,$org_h);
imagecopyresampled($tmp,$source,0,0,0,0,$orig_w,$org_h,$src_width,$src_height);
到
$org_h = ceil(($src_height/$src_width)* $org_w);
echo "h=$org_h<br />";
echo "w=$org_w<br />";
$tmp = imagecreatetruecolor($org_w,$org_h);
imagecopyresampled($tmp,$source,0,0,0,0,$org_w,$org_h,$src_width,$src_height);