Written by davidonzo on 30/10/2008, Filled in OpenSource, Web, Tutorial
WARNING!
This article has been written 499 days ago.
The informations may be out of dated!

php at davidonzo.comI'm trying to have time for a blog template restyling. In order to do it I write a simple file to create live png, jpeg and gif thumbelins.

 

This script require the GD compiled on your PHP installation.

 

<?php
   $file = $_GET['file'];
   $resize = $_GET['w'];
   $altez  = $_GET['h'];

    $cosa = getimagesize($file);
    $width = ($resize < $cosa[0]) ? $resize : $cosa[0];
    $alte = (!$altez) ? ceil(($cosa[1] * $width)/$cosa[0])+1 : $altez;
    $min  = imagecreatetruecolor($width, $alte);
     switch($cosa['mime']){
       case 'image/png':
         header('Content-Type: image/png');
         $im = imagecreatefrompng($file);
         imagealphablending($min, false);
         imagecopyresized($min, $im, 0, 0, 0, 0, $width, $alte, imageSx($im), imageSy($im));
         imagesavealpha($min, true);
         imagepng($min,'',8,PNG_ALL_FILTERS);
       break;
    
       case 'image/gif':
         header('Content-Type: image/png');
         $im = imagecreatefromgif($file);
         imagealphablending($min, false);
         $colorTransparent = imagecolorallocatealpha($min, 0, 0, 0, 127);
         imagefill($min, 0, 0, $colorTransparent);
         imagecopyresized($min, $im, 0, 0, 0, 0, $width, $alte, imageSx($im), imageSy($im));
         imagesavealpha($min, true);
         imagepng($min,'',8,PNG_ALL_FILTERS);
       break;
    
       case 'image/jpeg':
       case 'image/jpg':
         header('Content-Type: image/jpeg');
         $im = imagecreatefromjpeg($file);
         imagecopyresized($min, $im, 0, 0, 0, 0, $width, $alte, imageSx($im), imageSy($im));
         imagejpeg($min,'',85);      
       break;    
     }
      imagedestroy($min);
?>

 

How To Use It

Write this code into a file named resize.php (of course you can choose a different name) and launch it via GET execution using a link like this below.
 


http://example.com/resize.php?file=http://example.com/img.png&w=200&h=100

 

var $file - Required - string

Must be a valid image address. Just gif, png or jpeg supported.

 

var $w - Required - integer

Set the width value of the image, resizing it using the specified pixel. If $h is not used, the image will be resized maintaining the original proportion.

 

var $h - Optional - integer

Set the height value of the image. It can cause loss of the proportions of the original image. Be carefull using it.

 

TODO

The script doesn't manage exception like unreadable file, not found error and type not supported. ATM I don't need these function. Anyway, if someone want to write this part of the cose and share it, it will be apprecied.

Did you find interesting this article?
Subscribe my feed to be advised of any new post!
 
.Comments rss

No comments yet...



Comments could be moderated.
If you don't see it immediately published, please avoid to insert it one more time.
Thanks for your patient.