All Things Web 2.0
A PHP image manipulation class, aimed at generating thumbnails. It features the ability to resize by width, height, and percentage, create custom crops, or square crops from the center, rotate the image, and create Apple™-style reflections. It also features the ability to perform multiple manipulations per instance (also known as chaining), without the need to save and re-initialize the class with every manipulation.
This class comes in both PHP 4 and 5 flavors, but I highly recommend the PHP 5 version, due to the “__destruct()” function that became available in version 5. This helps ensure that you do not inadvertently create memory leaks by forgetting to call the “destruct()” function explicitly, like you must in the PHP 4 version of this script.
After the large amount of feedback that I got with the first version of this class, I did a lot of code refactoring, and simplified the API greatly. I have also added some new features:
For the uninitiated, the class still contains it’s old features:
Both PHP 5 & 4 Versions
Download PHP 4 v2.0
Download PHP 5 v2.0
Demo:
A demo of the script can be found here.
To begin, you need to create an instance of the class:
<?php include_once('thumbnail.inc.php'); $thumb = new Thumbnail('path/to/image/file.jpg'); ?>
Now you can begin your manipulations. The individual functions are described in detail below.
IMPORTANT PHP 4 NOTE
You must remember to call the destruct() function at then end of your scripts in order to deallocate the memory used for the image manipulation, similar to the way you must always call fclose() when you use fopen(). The PHP 5 class does this automatically, so there is no need to explicitly call this function (it doesn’t exist as a matter of fact!).
resize($maxWidth,$maxHeight)
Resizes the image to be now wider than maxWidth and no higher than maxHeight. Both values are required. Example:
//resize image to no wider than 250 pixels wide and 250 pixels high $thumb->resize(250,250);
resizePercent($percent)
Resizes the image to a percentage of its original size. Example:
//reduce the image by 50% $thumb->resizePercent(50);
cropFromCenter($cropSize)
Create a square crop of $cropSize pixels from the center of the image. Example:
//create a 100x100 pixel crop from the center of an image $thumb->cropFromCenter(100);
crop($startX,$startY,$width,$height)
Crop the image at $startX,$startY with a crop area of $width by $height pixels. Example:
//create a 100x50 pixel crop from the top left corner of an image $thumb->crop(0,0,100,50);
rotateImage($direction)
Rotates the image 90° either clock-wise (default) or counter clock-wise. Example:
//rotate the image clock-wise $thumb->rotateImage(); //also valid: $thumb->rotateImage('CW'); //rotate the image counter clock-wise $thumb->rotateImage('CCW');
createReflection($percent,$reflection,$white,$border,$borderColor)
Creates an Apple™-style reflection (it’s more of a web 2.0 thing now, I know…) from an image. This one’s a bit weird to explain, but here goes:
Here’s an example:
And the code that generated it:
$thumb->createReflection(40,40,80,true,'#a4a4a4');
This library is licensed under the MIT License. You can use it for whatever you?d like, as long as the copyright notices remain in place.
Feel free to submit any comments, or bugs below
15 Responses for "PHP Thumbnailer Class v2.0"
[…] üzerinde bir çok işlemi rahatlıkla yapacabileceğiniz bu kütüphaneyi kullanmakta çok kolay. Thumbnailer Class ile image dosyalarınızı farklı şekillerde yeniden boyutlandırabilir, kesme işlemleri […]
Hey guy, really GREAT script! Iam using it in 2 projects :-)) please keep on making it better ;-).
great job dude, also i see the php5 class and is very cool caz u use __construct and __destruct very nice dude, can i help in anything?
maybe i can be a mirro for ur phpthumb files, just le me know if i can help dude.
Hi, this PHP class is awesome.
I’m building a bespoke content management system and this has really taken the hassle out of creating thumbnails and maximum-sized images for my photo gallery module; it does everything I need.
Congratulations on producing something so, so simple and easy to use for something that’s so boring and meticulous.
Is it possible to save the generated image?
@Karl,
It’s totally possible, check out the class itself, you can use the save method:
$thumb->save(’/path/to/the/fileyouwanttosave.jpg’);
Thanks! Very usefull class! I like it! But if GIF have transparency, background became black. How to make it white? Thanks!
sorry..i have used your class..but i have a problem:
�PNG ��� IHDR����������’���� PLTE����������^������IDAT8��A �0ES!���;�9E\��`�#.=E���*: ��r����o�����¢M����9���p�Ќ��G5�Ә�b����8 �G�^���%��f���cD�4�9�ۮ� 6Ӽ��2′g��p��z9ݓ�\R���?�^�G�^|߶�^iޣ_���+n��3�����IEND�B`�
Fatal error: Cannot break/continue 1 level in /membri2/nephilimdie/asa/form/php/class/thumbnail.inc.php on line 173
do you know this?what is it?
thanks to help me!
It could be sweet if we can apply watermarks or images overlay. What do you think?
This class y very usefull to us who develop aplicattions in flash with php, because make our life so much easy with the managment of images.
Thanks and congratulations.
I get the following error when uploading GIF images.
Fatal error: Call to undefined function: imagegif() in /home/cust1/user1376116/html/dtools/classes/thumbnail.inc.php on line
Its weird because I checked the actual class and its happening within this function (”show”):
function show($quality=100,$name = ”) {
switch($this->format) {
case ‘GIF’:
if($name != ”) {
ImageGif($this->newImage,$name);
}
else {
header(’Content-type: image/gif’);
ImageGif($this->newImage);
}
break;
case ‘JPG’:
if($name != ”) {
ImageJpeg($this->newImage,$name,$quality);
}
else {
header(’Content-type: image/jpeg’);
ImageJpeg($this->newImage,”,$quality);
}
break;
case ‘PNG’:
if($name != ”) {
ImagePng($this->newImage,$name);
}
else {
header(’Content-type: image/png’);
ImagePng($this->newImage);
}
break;
}
}
Help anyone?
HI, Im using phpmaker and was told this is an extension for it. I cant get this working at all and cant find any documentation for adding an extension to phpmaker. can anyone tell how to add it to my extensions list within phpmaker? thanks.
danster, this isn’t an extension - you just need to use it as an include file. In the download you’ll see an example - take a look.
Here is a watermarking code requested above i needed it too:
Add BEFORE last curly bracket(})
++++++++++
public function watermark($imagesource){
$pathinfo = pathinfo($imagesource);
$var1 = $pathinfo[’extension’];
$var2 = “png”;
$var3 = “jpeg”;
$var4 = “jpg”;
$var5 = “gif”;
if(strcasecmp($var1, $var2) == 0){
$watermark = @imagecreatefrompng($imagesource);
}elseif((strcasecmp($var1, $var3) == 0) || (strcasecmp($var1, $var4) == 0)){
$watermark = @imagecreatefromjpeg($imagesource);
}elseif(strcasecmp($var1, $var5) == 0){
$watermark = @imagecreatefromgif($imagesource);
}
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = ($this->currentDimensions[’width’] - $watermarkwidth);
$startheight = ($this->currentDimensions[’height’] - $watermarkheight);
imagecopy($this->workingImage, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
}
++++++++++
USAGE
++++++++++
$thumb = new Thumbnail(”image/to/watermark.jpg”);
$thumb->resize(360,355);
$thumb->watermark(”path/to/watermark.png”);
$thumb->show();
++++++++++
all image file type accepted except bmp of coarse
Awesom - thanks a lot, Ian!
Leave a reply