Search code examples
phpimagefile-uploadthumbnailsimagecreatefrompng

Creating thumbnail of smaller size from existing image


I am uploading an image to my folder and I have created a thumbnail from uploaded image.

/*
save this image to try -- 
http://images.dpchallenge.com/images_challenge/0-999/319/800/Copyrighted_Image_Reuse_Prohibited_157378.jpg 
*/

    $source = 'H:/xampp/htdocs/myfolder/new.jpg';
    $destination = $_SERVER['DOCUMENT_ROOT'] . 'myfolder/New/';
    $quality = 60;
    $info = getimagesize($source);
    if ($info['mime'] == 'image/jpeg') {
        $image = imagecreatefromjpeg($source);
    }
    echo $image;
    print(imagejpeg($image, $destination, $quality));
    imagejpeg($image, $destination, $quality);

But, it is not creating anything at all, I alreday have uploaded image in

Source=> /myfolder/

Now, how to create a thumbnail of smaller size and save it to

destination=> myfolder/New/

or May be my approach is wrong, If so please tell what is the right way.

Any help is really appreciated.


Solution

  • Try this it will works for .jpg and .png images

    <?php
    $file = 'H:/xampp/htdocs/myfolder/phool.png';
    $pathToSave = 'H:/xampp/myfolder/New/';
    $what = getimagesize($file);
    $file_name = basename($file);
    //print "MIME ".$what['mime'];
    switch(strtolower($what['mime']))
    {
        case 'image/png':
                $img = imagecreatefrompng($file);
                $new = imagecreatetruecolor($what[0],$what[1]);
                imagecopy($new,$img,0,0,0,0,$what[0],$what[1]);
                header('Content-Type: image/png');
                imagepng($new,$pathToSave.$file_name,9);
                imagedestroy($new);
            break;
        case 'image/jpeg':
                $img = imagecreatefromjpeg($file);
                $new = imagecreatetruecolor($what[0],$what[1]);
                imagecopy($new,$img,0,0,0,0,$what[0],$what[1]);
                header('Content-Type: image/jpeg');   
                imagejpeg($new,$pathToSave.$file_name);
                imagedestroy($new);
            break;
        case 'image/gif':
            $img = imagecreatefromgif($file);
            break;
        default: die();
    }
    ?>