3

I have no idea how to resize image in PHP, my code is:

for ($index = 1; $index <= 2; $index++) { 

    if (!empty($_FILES["pic$index"]["name"])) {
        $ext = substr($_FILES["pic$index"]["name"], strrpos($_FILES["pic$index"]["name"], '.') + 1);
        $dir = "../gallery/$mkdir";

        HERE I NEED THE RESIZE OF THE TMP FILE OF IMAGE

        move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext");

    }  

}

$mkdir = the name of the gallery's folder (there are many galleries).

$dir = where the pics will be placed.

$ext = the type of the image (png, gif or jpg).

foreach loop runs two times because you can upload two pics.

This script is working good, I just need to do resize and I dont have an idea how to do it..

Luis
  • 2,949
  • 12
  • 43
  • 58
  • 2
    Note that you cannot resize an image before uploading it - it needs uploading so that php can resize it. – Adam Hopkinson Sep 24 '10 at 12:25
  • are you sure? so I have to upload it and resize it and after again upload again? – Luis Sep 24 '10 at 12:26
  • Hey Luis, May I ask...why not allow the full image size to be uploaded and resize the image as the page renders? – d2burke Sep 24 '10 at 12:30
  • Hey, because the size of 600*600 image will be bigger than 200*200 for example.. – Luis Sep 24 '10 at 12:32
  • Not sure I follow. Do you want all of the images to display at a certain size when they are output? If so, you can actually resize them accordingly as the page renders with classes like PHPThumb. – d2burke Sep 24 '10 at 12:37
  • 3
    `move_uploaded_file()` **do not upload anything**. It just move a file from one directory to another **on the same server, after** upload. – Your Common Sense Sep 24 '10 at 13:08
  • @Col. Shrapnel - I'm understanding, good point thank you. – Luis Sep 24 '10 at 13:21
  • Possible duplicate of [resize image in PHP](http://stackoverflow.com/questions/14649645/resize-image-in-php) – Kevin Brown Jan 01 '17 at 17:46

2 Answers2

12

Here is the code I'm using to resize images.

In my case I give to the function the original file name and then the thumbnail file name.

You can adapt it for your case very easily.

public static function GenerateThumbnail($im_filename,$th_filename,$max_width,$max_height,$quality = 0.75)
{
// The original image must exist
if(is_file($im_filename))
{
    // Let's create the directory if needed
    $th_path = dirname($th_filename);
    if(!is_dir($th_path))
        mkdir($th_path, 0777, true);
    // If the thumb does not aleady exists
    if(!is_file($th_filename))
    {
        // Get Image size info
        list($width_orig, $height_orig, $image_type) = @getimagesize($im_filename);
        if(!$width_orig)
            return 2;
        switch($image_type)
        {
            case 1: $src_im = @imagecreatefromgif($im_filename);    break;
            case 2: $src_im = @imagecreatefromjpeg($im_filename);   break;
            case 3: $src_im = @imagecreatefrompng($im_filename);    break;
        }
        if(!$src_im)
            return 3;


        $aspect_ratio = (float) $height_orig / $width_orig;

        $thumb_height = $max_height;
        $thumb_width = round($thumb_height / $aspect_ratio);
        if($thumb_width > $max_width)
        {
            $thumb_width    = $max_width;
            $thumb_height   = round($thumb_width * $aspect_ratio);
        }

        $width = $thumb_width;
        $height = $thumb_height;

        $dst_img = @imagecreatetruecolor($width, $height);
        if(!$dst_img)
            return 4;
        $success = @imagecopyresampled($dst_img,$src_im,0,0,0,0,$width,$height,$width_orig,$height_orig);
        if(!$success)
            return 4;
        switch ($image_type) 
        {
            case 1: $success = @imagegif($dst_img,$th_filename); break;
            case 2: $success = @imagejpeg($dst_img,$th_filename,intval($quality*100));  break;
            case 3: $success = @imagepng($dst_img,$th_filename,intval($quality*9)); break;
        }
        if(!$success)
            return 4;
    }
    return 0;
}
return 1;
}

The return codes are just here to differentiate between different types of errors.

By looking back at that code, I don't like the "magic number" trick. I'm gonna have to change that (by exceptions for example).

if (!empty($_FILES["pic$index"]["name"])) {
    $ext = substr($_FILES["pic$index"]["name"], strrpos($_FILES["pic$index"]["name"], '.') + 1);
    $dir = "../gallery/$mkdir";
    // Move it
    if(move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext.tmp"))
    {
      // Resize it
      GenerateThumbnail("$dir/img-$index.$ext.tmp","$dir/img-$index.$ext",600,800,0.80);
      // Delete full size
      unlink("$dir/img-$index.$ext.tmp");
    }
} 

Use move_uploaded_file to move it (recommanded) and then you can resize it and send it to it's final destination. You might not even need the ".tmp", you can use.

    // Move it
    if(move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext"))
    // Resize it
      GenerateThumbnail("$dir/img-$index.$ext","$dir/img-$index.$ext",600,800); 
Loïc Février
  • 6,794
  • 7
  • 37
  • 50
  • what is this th_filename and im_filename? in addition, can i remove the part of "// Let's create the directory if needed.."?. – Luis Sep 24 '10 at 12:30
  • It's the name of the image file and the name of the thumbnail file. "Takes that image, do a thumbnail and save it there". You can delete the part for the directory if you're sure it already exists. – Loïc Février Sep 24 '10 at 12:35
  • Are you sure the function is fine? you cant write: if(!$src_im) return 3; for example. – Luis Sep 24 '10 at 12:48
  • Ok if you say i trust you. :-) But i dont understand really the function.. the return numbers (0,1,2,3,4) - why? and where I mix it in my code? – Luis Sep 24 '10 at 12:53
  • In my code 0 means success, other value means error. Each value is a different error. Using "magic numbers" like that is not the best way to do it, this is old code, not touched for a long time. You can replace 0 by true and any other value by false, if you don't want to know what was the error exactly. – Loïc Février Sep 24 '10 at 12:56
  • That I understood, but I dont the meaning of the function... if I upload the file for example "pic1.png" -> it is placed in $_FILES["pic1"]["tmp_name"] for example. the function is change his size and save it in $_FILES["pic1"]["tmp_name"]? – Luis Sep 24 '10 at 13:00
  • And what do I write in my code? $this->GenerateThumbnail($_FILES["pic$index"]["tmp_name"],$_FILES["pic$index"]["tmp_name"],200,200,$quality = 0.75).. or? thank you very much by the way :-) – Luis Sep 24 '10 at 13:01
  • why to move before resize? it's nonsense – Your Common Sense Sep 24 '10 at 13:06
  • Hum, because move_uploaded_file is supposed to do some verifications ? (http://php.net/manual/fr/function.move-uploaded-file.php) Otherwise we could use "rename". At least that's how I understand it. – Loïc Février Sep 24 '10 at 13:08
  • I think the OP wants not to create a thumbnail, but resize a source image itself. – Your Common Sense Sep 24 '10 at 13:09
  • Correct but calling the function with the same name for source and destination will resize the image. IF different names, thumbnail. – Loïc Février Sep 24 '10 at 13:11
  • Loïc you are the best! Thank you very much it's working. By the way I have to learn this subject (working with GD..). thank you again. – Luis Sep 24 '10 at 13:15
  • 1
    @Luis : I've modified the function to test for return value of move_uploaded_file – Loïc Février Sep 24 '10 at 13:17
  • I want to press up arrow for one hundred times, @LoïcFévrier thank u – Husam May 03 '15 at 19:36
1

Keep in mind that the picture you are dealing with is already uploaded on the server. You actualy want to resize picture before storing it in "safe place".

$_FILES["pic$index"]["tmp_name"] is probably /tmp/somepicturesname

Aif
  • 10,331
  • 1
  • 27
  • 41