帮助使用函数调整图像大小

发布于 2024-11-06 06:19:53 字数 3096 浏览 2 评论 0原文

在下面的代码中,我尝试在上传图像后调整图像的大小。使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洛阳烟雨空心柳 2024-11-13 06:19:53

好的,首先,你面临的问题...

给定一个有效的 mime 类型,你的脚本将上传的文件移动到

'uploads/' . $name

Therefor, $tname 不再是有效的路径,但你仍然将它传递给 thumb () 函数,用于使用 GD createfrom* 函数之一创建图像资源。

这显然不起作用,因为 $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!.';
}

在这条线上

如果($调整大小== TRUE)

很有可能 $resize 未定义。您至少应该在顶部将其初始化为 false

Ok first, your immediate problem...

Given a valid mime type, your script moves the uploaded file to

'uploads/' . $name

Therefor, $tname is no longer a valid path yet you still pass it to the thumb() function where it's used to create an image resource using one of the GD createfrom* functions.

This obviously cannot work as there is no image at $tname.


Other problems...

Your first switch block can be reduced to

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!.';
}

On this line

if ($resize == TRUE)

there's a good chance $resize is undefined. You should at least initialise it to false at the top

仲春光 2024-11-13 06:19:53

有几个问题,但这就是阻止拇指功能正常工作的原因:

更改

$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);

Couple of problems, but this is what is stopping the thumb function from working properly:

Change

$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);

to

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文