1

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.

Naincy
  • 2,953
  • 1
  • 12
  • 21
Hitesh
  • 4,098
  • 11
  • 44
  • 82

4 Answers4

2

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();
}
?>
Dhanendran
  • 382
  • 3
  • 13
0

I have always found image manipulation a pain so I use a library called Intervention.

Here is example to create a thumbnail based on your scenario:

// open an image file
$img = Image::make('H:/xampp/htdocs/myfolder/new.jpg');
// now you are able to resize the instance
$img->resize(75, 75);
// save the image as a new file
$img->save($destination.'new.jpg');
David Jones
  • 4,275
  • 6
  • 27
  • 51
0

you can do it like this

list($sourceWidth,$sourceHeight) = getimagesize($filepath)
$destWidth = round($sourceWidth/2);
$destHeight = round($sourceHeight/2);
$sourceImage = imagecreatefromjpeg($filepath); 
$destinationImage = imagecreatefromtrue($destWidth,$destHeight);

//Now Main function for resizing purpose 
   imagecopyresized($destinationImage,$sourceImage,0,0,0,0,$destWidth,$destHeight,$sourceWidth,$sourceHeight);

// Now You can save Image 
imagejpeg($destImage,$pathToSave);

Try this

justrohu
  • 595
  • 3
  • 9
0

can this function help you:

function makeThumbnails($updir, $img, $id)
{
    $thumbnail_width = 134;
    $thumbnail_height = 189;
    $thumb_beforeword = "thumb";
    $arr_image_details = getimagesize("$updir" . $id . '_' . "$img"); // pass id to thumb name
    $original_width = $arr_image_details[0];
    $original_height = $arr_image_details[1];
    if ($original_width > $original_height) {
        $new_width = $thumbnail_width;
        $new_height = intval($original_height * $new_width / $original_width);
    } else {
        $new_height = $thumbnail_height;
        $new_width = intval($original_width * $new_height / $original_height);
    }
    $dest_x = intval(($thumbnail_width - $new_width) / 2);
    $dest_y = intval(($thumbnail_height - $new_height) / 2);
    if ($arr_image_details[2] == 1) {
        $imgt = "ImageGIF";
        $imgcreatefrom = "ImageCreateFromGIF";
    }
    if ($arr_image_details[2] == 2) {
        $imgt = "ImageJPEG";
        $imgcreatefrom = "ImageCreateFromJPEG";
    }
    if ($arr_image_details[2] == 3) {
        $imgt = "ImagePNG";
        $imgcreatefrom = "ImageCreateFromPNG";
    }
    if ($imgt) {
        $old_image = $imgcreatefrom("$updir" . $id . '_' . "$img");
        $new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
        imagecopyresized($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
        $imgt($new_image, "$updir" . $id . '_' . "$thumb_beforeword" . "$img");
    }
}
tinos
  • 562
  • 6
  • 12