/ Published in: PHP
* Creates thumbnail from image
* thumbnail dimensions can be set as parameters
* accepts JPG, JPEG, GIF, PNG
* preserves transparency for GIF and PNG on thumbnails
* call function like this: createthumb(images/original.jpg, thumbs/thumb.jpg, 80, 80);
* thumbnail dimensions can be set as parameters
* accepts JPG, JPEG, GIF, PNG
* preserves transparency for GIF and PNG on thumbnails
* call function like this: createthumb(images/original.jpg, thumbs/thumb.jpg, 80, 80);
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Function that creates thumbnail from image * Original author: Christian Heilmann * Fixed and extended: Ales Rebec * * @param $name Original filename (fullpath to image) * @param $filename Filename of the resized image (fullpath to thumbnail that will be created) * @param $new_w width of resized image in px (ex. 80 or 100) * @param $new_h height of resized image in px */ public function createthumb($name,$filename,$new_w,$new_h) { $type = 0; $mime = $size['mime']; //get image mime type (ex. "image/jpeg" or "image/gif") if (preg_match("/jpg|jpeg/i",$system[sizeof($system)-1])) {$type=1; $src_img=imagecreatefromjpeg($name);} if ($old_x > $old_y) //calculate thumnails dimenstions and preserve aspect ratio { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } if($type > 1) $this->setTransparency($dst_img, $src_img); //if GIF or PNG -> set tranparency }