Jump to content

Image resize/upload


oMIKEo

Recommended Posts

Ive got this script that should take an uploaded image and resize it. The script works if the image is exactly the set max dimention or lower but not if the image is larger.

 

Can anyone point out where i am going wrong?

 

 

HERE IS THE INITIAL SCRIPT

<?php
if ($_FILES['photo']['name'] != "") { 

list($width, $height, $type, $attr) = getimagesize($_FILES['photo']['tmp_name']); 

do { 
      $photo1 = randomstring(16); 
    } while (file_exists("../i/news/" . $photo1)); 

$types = array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF(intel byte order)', 8 => 'TIFF(motorola byte order)', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM');
   
   $photo1 = $photo1 . '.' . $types[$type];

// Automatic resize
handleUploadedFile($photo1, $_FILES['photo']['tmp_name'], '../i/news/');
}
?>

 

HERE IS THE FUNCTION

<?php
function make_seed() { 
 list($usec, $sec) = explode(' ', microtime());
 return (float) $sec + ((float) $usec * 100000);
}
  
function randomstring( $stringlength, $allowchars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ) { 
        $output = ""; 
        mt_srand(make_seed()); 
        $count = strlen( $allowchars ); 
        for( $i = 0; $i < $stringlength; $i++ ) { 
            $output .= $allowchars[ mt_rand( 0, $count - 1 ) ]; 
        } 
        return $output; 
} 

function handleUploadedFile($file_name, $tmp_name, $target_path){ 
    // $file_name = the name that the file is going to be stored as on the server. 
    // $tmp_name = the temporary storage place of the file on the server. 
    // $target_path =  Where the file is going to be placed  
     
    $no_support = false; 
    $error = ""; 
    $target_path = $target_path . $file_name;        // PUT THE TARGET DIRECTORY AND FILE NAME TOGETHER. 

    // CHECK IF FILE NEEDS TO BE RESIZED. 
    $max_dimensions = 250; 
    list($width, $height, $type, $attr) = getimagesize($tmp_name); // GET FILE INFO OF THE TEMPORARY FILE. 
         
    // CHECK IF THE TYPE OF THE UPLOADED IMAGE IS SUPPORTED BY THE SERVER. 
    // IF SO... CREATE AN IMAGE FROM THE TEMPORARY FILE. 
    if($type==1){ 
        if(function_exists("imagecreatefromgif") == false OR function_exists("imagegif") == false){ 
            $no_support = true; 
            $error="No support for this GIF image."; 
        } else{ 
            $src_image = imagecreatefromgif($tmp_name);     
        } 
    } else if($type==2){ 
        if(function_exists("imagecreatefromjpeg") == false OR function_exists("imagejpeg") == false){ 
            $no_support = true; 
            $error="No support for this JPEG image."; 
        } else{ 
            $src_image = imagecreatefromjpeg($tmp_name);     
        } 
    } else if($type==3){ 
        if(function_exists("imagecreatefrompng") == false OR function_exists("imagepng") == false){ 
            $no_support = true; 
            $error="No support for this PNG image."; 
        } else{ 
            $src_image = imagecreatefrompng($tmp_name);     
        } 
    } else{ 
        $error="This image-type is not supported."; 
    } 

    // CHECK IF THE IMAGE NEED TO BE RESIZED. 
    if($width <= $max_dimensions AND $height <= $max_dimensions){ 
        if($no_support != true){  // IF SUPPORTED.. UPLOAD IMAGE. 
            // ... UPLOAD FILE. 

		move_uploaded_file($tmp_name, $target_path);

        } 
        if(isset($src_image) == true){imagedestroy($src_image);} 
        return "right_size"; 
    } 

    // IF AN ERROR OCCURED.. END THE FUNCTION BY RETURING THE ERROR. 
    if($error != ""){ 
        imagedestroy($src_image); 
        return $error; 
    } 

    // DETERMINE THE NEW IMAGE DIMENSIONS. 
    if($width > $height){ 
        $new_width=$max_dimensions;                    // MAX WIDTH OF THE IMAGE. 
        $new_height=($height/$width)*$new_width;    // CALCULATE THE APPROPRIATE HEIGHT OF THE IMAGE. 
    } else{ 
        $new_height=$max_dimensions;                // MAX HEIGHT OF THE IMAGE. 
        $new_width=($width/$height)*$new_height;    // CALCULATE THE APPROPRIATE WIDTH OF THE IMAGE. 
    } 

    // RESIZE THE IMAGE. 
    if (function_exists("imagecopyresampled") && function_exists("imagecreatetruecolor")){ 
        $new_image=imagecreatetruecolor($new_width,$new_height); 
        imagecopyresampled($new_image,$src_image,0,0,0,0,$new_width,$new_height,$width,$height); 
    } else {  // IF imagecopyresample DOESNT EXIST USE imecopyresized WHICH GIVES LOWER QUALITY. 
        $new_image=imagecreate($new_width,$new_height); 
        imagecopyresized($new_image,$src_image,0,0,0,0,$new_width,$new_height,$width,$height); 
    } 
     
    // SAVE THE FILE TO THE SERVER. 
    switch ($type){  
    case 1:    // TYPE IS gif 
        imagegif($new_image,$target_path,100); 
        break; 
    case 2: // TYPE IS jpg 
        imagejpeg($new_image,$target_path,100); 
        break; 
    case 3: // TYPE IS png 
        imagepng($new_image,$target_path,100); 
        break; 
    } 

    // DELETE THE IMAGES FROM THE TEMPORARY STORAGE PLACE. 
    imagedestroy($src_image); 
    imagedestroy($new_image); 

    return ""; 
}  


?>

 

Thanks,

Mike

Link to comment
Share on other sites

If im reading this correctly, your telling php too check if it's small or equal too max dimentions,

 

Replace your Handle function with the following code

function handleUploadedFile($file_name, $tmp_name, $target_path){ 
    // $file_name = the name that the file is going to be stored as on the server. 
    // $tmp_name = the temporary storage place of the file on the server. 
    // $target_path =  Where the file is going to be placed  
     
    $no_support = false; 
    $error = ""; 
    $target_path = $target_path . $file_name;        // PUT THE TARGET DIRECTORY AND FILE NAME TOGETHER. 

    // CHECK IF FILE NEEDS TO BE RESIZED. 
    $max_dimensions = 250; 
    list($width, $height, $type, $attr) = getimagesize($tmp_name); // GET FILE INFO OF THE TEMPORARY FILE. 
         
    // CHECK IF THE TYPE OF THE UPLOADED IMAGE IS SUPPORTED BY THE SERVER. 
    // IF SO... CREATE AN IMAGE FROM THE TEMPORARY FILE. 
    if($type==1){ 
        if(function_exists("imagecreatefromgif") == false OR function_exists("imagegif") == false){ 
            $no_support = true; 
            $error="No support for this GIF image."; 
        } else{ 
            $src_image = imagecreatefromgif($tmp_name);     
        } 
    } else if($type==2){ 
        if(function_exists("imagecreatefromjpeg") == false OR function_exists("imagejpeg") == false){ 
            $no_support = true; 
            $error="No support for this JPEG image."; 
        } else{ 
            $src_image = imagecreatefromjpeg($tmp_name);     
        } 
    } else if($type==3){ 
        if(function_exists("imagecreatefrompng") == false OR function_exists("imagepng") == false){ 
            $no_support = true; 
            $error="No support for this PNG image."; 
        } else{ 
            $src_image = imagecreatefrompng($tmp_name);     
        } 
    } else{ 
        $error="This image-type is not supported."; 
    } 

    // CHECK IF THE IMAGE NEED TO BE RESIZED. 
    if($width >= $max_dimensions AND $height >= $max_dimensions){ 
        if($no_support != true){  // IF SUPPORTED.. UPLOAD IMAGE. 
            // ... UPLOAD FILE. 

		move_uploaded_file($tmp_name, $target_path);

        } 
        if(isset($src_image) == true){imagedestroy($src_image);} 
        return "right_size"; 
    } 

    // IF AN ERROR OCCURED.. END THE FUNCTION BY RETURING THE ERROR. 
    if($error != ""){ 
        imagedestroy($src_image); 
        return $error; 
    } 

    // DETERMINE THE NEW IMAGE DIMENSIONS. 
    if($width > $height){ 
        $new_width=$max_dimensions;                    // MAX WIDTH OF THE IMAGE. 
        $new_height=($height/$width)*$new_width;    // CALCULATE THE APPROPRIATE HEIGHT OF THE IMAGE. 
    } else{ 
        $new_height=$max_dimensions;                // MAX HEIGHT OF THE IMAGE. 
        $new_width=($width/$height)*$new_height;    // CALCULATE THE APPROPRIATE WIDTH OF THE IMAGE. 
    } 

    // RESIZE THE IMAGE. 
    if (function_exists("imagecopyresampled") && function_exists("imagecreatetruecolor")){ 
        $new_image=imagecreatetruecolor($new_width,$new_height); 
        imagecopyresampled($new_image,$src_image,0,0,0,0,$new_width,$new_height,$width,$height); 
    } else {  // IF imagecopyresample DOESNT EXIST USE imecopyresized WHICH GIVES LOWER QUALITY. 
        $new_image=imagecreate($new_width,$new_height); 
        imagecopyresized($new_image,$src_image,0,0,0,0,$new_width,$new_height,$width,$height); 
    } 
     
    // SAVE THE FILE TO THE SERVER. 
    switch ($type){  
    case 1:    // TYPE IS gif 
        imagegif($new_image,$target_path,100); 
        break; 
    case 2: // TYPE IS jpg 
        imagejpeg($new_image,$target_path,100); 
        break; 
    case 3: // TYPE IS png 
        imagepng($new_image,$target_path,100); 
        break; 
    } 

    // DELETE THE IMAGES FROM THE TEMPORARY STORAGE PLACE. 
    imagedestroy($src_image); 
    imagedestroy($new_image); 

    return ""; 
}  

 

Sorry if i miss understood the question :)

 

Stuie

Link to comment
Share on other sites

That hasnt worked, that has made it so if the image is smaller it is increased to the max dimention so the image gets distorted. And still wont process images that are larger. Exact images get uploaded fine. (thanks for the attempt though)

 

Any other ideas??

Link to comment
Share on other sites

what is the value coming for $new_height and $new_width and also r u getting the right $width and $height values. you can echo them and check that out.. otherwise the code looks ok...

 

// DETERMINE THE NEW IMAGE DIMENSIONS.

    if($width > $height){

        $new_width=$max_dimensions;                    // MAX WIDTH OF THE IMAGE.

        $new_height=($height/$width)*$new_width;    // CALCULATE THE APPROPRIATE HEIGHT OF THE IMAGE.

    } else{

        $new_height=$max_dimensions;                // MAX HEIGHT OF THE IMAGE.

        $new_width=($width/$height)*$new_height;    // CALCULATE THE APPROPRIATE WIDTH OF THE IMAGE.

    }

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.