开发者

Help with resizing a image with a function

开发者 https://www.devze.com 2023-03-03 21:40 出处:网络
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.

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_t开发者_开发百科o_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!!!


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


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

精彩评论

暂无评论...
验证码 换一张
取 消