0

i have searched the net for quite so long, but i have seen that there are uploading images that are capable of the task, but i was wondering, if there's a way that i could upload an image then automatically converts the image uploaded in different thumbnail sizes, like 50x50, 150x150, 250x250.

Thanks in Advance.

Hades Hades
  • 61
  • 1
  • 9
  • http://www.9lessons.info/2009/03/upload-and-resize-image-with-php.html go to there? – Abdul Muheet Feb 28 '15 at 04:11
  • This class will make any thumb size you want: http://stackoverflow.com/questions/28002244/crop-resize-image-function-using-gd-library/28008400#28008400 – Rasclatt Feb 28 '15 at 04:24

2 Answers2

1

Sure.

If you have GD installed on your server you can use http://php.net/manual/en/book.image.php

If you have imagemagick you can use http://php.net/manual/en/book.imagick.php

Here is a quick example using GD. This assumes you're uploading the image using an <input type="file" name="FileUploadName"> element.

$uploadedFilePath = $_FILES["FileUploadName"]["tmp_name"];
$somePermanentPath = "/tmp/mynewfile";
move_uploaded_file($uploadedFilePath, $somePermanentPath);
$srcImg = imagecreatefromstring(file_get_contents($somePermanentPath));
$srcW   = imagesx($srcImg);
$srcH   = imagesy($srcImg);
if ($srcW > $srcH)
{
    $widthFactor  = 1;
    $heightFactor = $srcH / $srcW;
}
else
{
    $heightFactor = 1;
    $widthFactor  = $srcW / $srcH;
}
$width50   = 50 * $widthFactor;
$height50  = 50 * $heightFactor;
$width150   = 150 * $widthFactor;
$height150  = 150 * $heightFactor;

$thumb50 = imagecreatetruecolor(50, 50);    
$thumb150 = imagecreatetruecolor(150, 150);    

imagecopyresampled($thumb50, $srcImg, 0, 0, 0, 0, 50, 50, $srcW, $srcH); //the 0's can be changed to deal with centering
imagecopyresampled($thumb150, $srcImg, 0, 0, 0, 0, 50, 50, $srcW, $srcH); //the 0's can be changed to deal with centering


//save as png
imagepng($thumb50, "/tmp/somefinallocation50.png");
imagepng($thumb150, "/tmp/somefinallocation150.png");

Note: you'll have to add extra logic for centering and creating backround whitespace, if you want to deal with that stuff.

Acknowledgement: This example was taken from ActiveWAFL's DblEj\Multimedia\Image::ResampleAndSave() method, with some code removed for ease of use.

Evan de la Cruz
  • 1,966
  • 1
  • 13
  • 17
0

This is a function which you can use to resize your image after installed imagick. No matter it is gif or jpeg.

 function resize($srcfile, $newWidth, $newHeight, $newfile){
        list($srcW, $srcH) = getimagesize($srcfile);
        $newWH = $srcWH = $srcW / $srcH;
        if($newHeight) $newWH = $newWidth / $newHeight;
        if($srcW > $newWidth || ($newHeight && $srcH > $newHeight)){
            if($newWH <= $srcWH){
                $ftoW = $newWidth;
                $ftoH = $ftoW * ($srcH / $srcW);
            }else{
                $ftoH = $newHeight;
                $ftoW = $ftoH * ($srcW / $srcH);
            }
            $thumb = new Imagick($srcfile);
            $thumb->resizeImage($ftoW, $ftoH, substr($srcfile, -3) == 'gif' ? Imagick::FILTER_CUBIC : imagick::FILTER_UNDEFINED, 1);
            $thumb->setCompression(Imagick::COMPRESSION_JPEG);
            $thumb->setCompressionQuality(50);
            $thumb->writeImage($newfile);
            $thumb->destroy();
            Return array($ftoW, $ftoH);
        }else{
            $thumb = new Imagick($srcfile);
            if(substr($srcfile, -3) == 'gif') $thumb->resizeImage($srcW, $srcH, Imagick::FILTER_CUBIC, 1);
            //$thumb->setCompression(Imagick::COMPRESSION_JPEG);
            //$thumb->setCompressionQuality(50);
            $thumb->writeImage($newfile);
            $thumb->destroy();
            Return array($srcW, $srcH);
        }
    }
harrrrrrry
  • 13,643
  • 2
  • 23
  • 28