帮助使用函数调整图像大小
在下面的代码中,我尝试在上传图像后调整图像的大小。使用 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;
}
}
为什么它不调整它的大小并将其放入文件夹中?卡住!!!
In the code below I am trying to resize a image after I upload it. All of the checks done with echo statements get the right data but it will not resize it and put it in the folder.
<?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);
}
?>
and here is the function I am calling:
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;
}
}
Why won't it resize it and put it in the folder?? STUCK!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,首先,你面临的问题...
给定一个有效的 mime 类型,你的脚本将上传的文件移动到
Therefor,
$tname
不再是有效的路径,但你仍然将它传递给thumb ()
函数,用于使用 GDcreatefrom*
函数之一创建图像资源。这显然不起作用,因为
$tname
处没有图像。其他问题...
您的第一个开关块可以简化为
在这条线上
很有可能
$resize
未定义。您至少应该在顶部将其初始化为false
Ok first, your immediate problem...
Given a valid mime type, your script moves the uploaded file to
Therefor,
$tname
is no longer a valid path yet you still pass it to thethumb()
function where it's used to create an image resource using one of the GDcreatefrom*
functions.This obviously cannot work as there is no image at
$tname
.Other problems...
Your first switch block can be reduced to
On this line
there's a good chance
$resize
is undefined. You should at least initialise it tofalse
at the top有几个问题,但这就是阻止拇指功能正常工作的原因:
更改
为
Couple of problems, but this is what is stopping the thumb function from working properly:
Change
to