I have been trawling around the internet looking for some simple functions for do basic automated manipulation to some images im using.
if you want to skip straight to the examples just to prove im not bulls**ting skip to the bottom
Basically i wanted to have these functions:
1. thumbnail
2. resize (actual image)
3. reduce (image file size)
4. rotate (by degrees)
Most of the scripts i found couldnt be changed easily to make them do what i wanted…
So here are what i got working.. and a few instructions…
Create a thumbnail
// Resample the image.
$tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");
// Create the new file name.
$newNameE = explode(".", $img);
$newName = ”. $newNameE[0] .”. $suffix .‘.’. $newNameE[1] .”;
// Save the image.
imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");
// Clean up.
imagedestroy($original);
imagedestroy($tempImg);
return true;
}
?>
Rotate
function rotateImage($img, $imgPath, $suffix, $degrees, $quality, $save)
{
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original");
list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
// Resample the image.
$tempImg = imagecreatetruecolor($width, $height) or die("Cant create temp image");
imagecopyresized($tempImg, $original, 0, 0, 0, 0, $width, $height, $width, $height) or die("Cant resize copy");
// Rotate the image.
$rotate = imagerotate($original, $degrees, 0);
// Save.
if($save)
{
// Create the new file name.
$newNameE = explode(".", $img);
$newName = ”. $newNameE[0] .”. $suffix .‘.’. $newNameE[1] .”;
// Save the image.
imagejpeg($rotate, "$imgPath/$newName", $quality) or die("Cant save image");
}
// Clean up.
imagedestroy($original);
imagedestroy($tempImg);
return true;
}
?>
Resize
function resizeImage($img, $imgPath, $suffix, $by, $quality)
{
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<em>$imgPath/$img</em>)");
list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
// Determine new width and height.
$newWidth = ($width/$by);
$newHeight = ($height/$by);
// Resample the image.
$tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");
// Create the new file name.
$newNameE = explode(".", $img);
$newName = ”. $newNameE[0] .”. $suffix .‘.’. $newNameE[1] .”;
// Save the image.
imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");
// Clean up.
imagedestroy($original);
imagedestroy($tempImg);
return true;
}
?>
Reduce
function reduceImage($img, $imgPath, $suffix, $quality)
{
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<em>$imgPath/$img</em>)");
list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
// Resample the image.
$tempImg = imagecreatetruecolor($width, $height) or die("Cant create temp image");
imagecopyresized($tempImg, $original, 0, 0, 0, 0, $width, $height, $width, $height) or die("Cant resize copy");
// Create the new file name.
$newNameE = explode(".", $img);
$newName = ”. $newNameE[0] .”. $suffix .‘.’. $newNameE[1] .”;
// Save the image.
imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");
// Clean up.
imagedestroy($original);
imagedestroy($tempImg);
return true;
}
?>
The simple instructions I used for the examples
Those are the function calls i used to create the images linked to below.
createThumbnail variables
image file
image file path
suffix to append to the name before the .jpg
thumbnail width
thumbnail height
thumbnail quality
rotateImage variables
image file
image file path
suffix to append to the name before the .jpg
degrees to rotate (anticlockwise i think) by
image quality
save or not (if you wanted to output to a temp to preview, i dont use it, so leave as 1
resize variables
image file
image file path
suffix to append to the name before the .jpg
resize by (i used 2 which i thought was half.. not so hot on the maths)
image quality
reduce variables
image file
image file path
suffix to append to the name before the .jpg
quality (100 best, 0 worst… Duhh).
Here are the links to my examples
Original File (904k (1840×1232))
Thumbnail (16k (120×100))
Resized and Reduced (24k (368×246))





48 Responses for "Image Manipulation using PHP"
[...] For those wanting to learn how to do some image manipulation with PHP, I’ve came across a great tutorial. [...]
i used your functions and there seems to be a problem with converting large images, i mean over the standard 1024×768.
any suggestions? apart from that hiccup they are pretty good.
Very good, I like this code!!!
I have only one question, can you make this code work for other image types, such as BMP, GIF, WMF, PNG …?
I really need a code for most of the image formats.
Greetings from Macedonia
Hi there
when i saw this example i thought… this is exatly what i was looking for. however i need to be able to resize images from a database rather that a specific path on the director. can you help in any way?
Thank you for your feedback everyone
I am working on an Imagemagick version of these functions, that will allow pretty much any format to be used.
This also seems to solve the large image problem you are having Artie.
As for resizing an image from the database, i will look into that one, but i cant promise anything
I woudl suggest taking the images out of the database and store them in normal folders, by userid/username if you are storing them there for userid purposes.
As i said i will look into it.
Jamie.
Cool, I like it
thank you
Can i do this whitout gd lib ? or 1.x… cause this works with gd 2.x+
I’m not sure about version numbers…
For the sake of this tutorial i was assuming that what gets installed by PHP is the default system.
My local system is using PHP4 and it works perfectly, better than my production environment oddly.
Jamie.
I agree, it has problems with large images. The page crashes with a 1200×1600 image
Adam,
I published an ImageMagicK version of these function a couple of days ago which are worth a look, as you are not constrained to JPEG images or image sizes below: 1200×1600.
Follow this link:
http://nodstrum.com/2007/02/06/imagemagick_manipulation/
Hi. When i saw this, this is exaclty what i was looking for. I am not very good with programming. When i tried this, all i get is a blank screen.
Hi Nathaniel,
These functions save the image into the directory you specify, they do not output the image to the screen.
$img must be an image reference, eg:
$saveImgPath = '/path/to/saveDir'; // no forward slash. $img = fopen('/path/to/image/theImage.jpg', 'r'); $ct = createThumbnail($img, $saveImgPath, "-thumb", 120, 100, 100);Hope that clarifies and helps.
Jamie.
[...] So there were a couple of problems with my last image processing functions (http://nodstrum.com/2006/12/09/image-manipulation-using-php/). [...]
[...] Quite a few people seemed to like my previous tutorial Image Manipulation using PHP, and looking around many of the tutorial sites there are hardly any Calendar systems. Perfect, here [...]
Thx so much, this code was a huge help to me, i appreciate it
Hi! This functions were very helpfull thank you.
But i noticed that the reduce function takes some tome to reduce the image quality, so I modified the code a little to prevent reducing the image quality if the reduced image has been created before.
// Create the new file name.
$newNameE = explode(“.”, $img);
$newName = ”. $newNameE[0] .’_’. $suffix .’_’. $quality .’.’. $newNameE[1] .”;
if (!file_exists($imgPath . $newName)) {
//the rest of the code goes here
return true;
} else {
return true;
}
elo. great script but i have one question.
is there any posibility to resize img proportionaly ? with only on dimension specified ?
when you have this error message is : undefined function imagecreatefromjpeg();
Solution:
my php install dir is: c:/php/
first you must try to find:
c:/php/php.ini
c:/php/ext/php_gd2.dll(php 5)
c:/php/extension/php_gd2.dll(php 4)
The php_gd2.dll is included in a standard PHP installation for Windows,
however, it’s not enabled by default.
You have to turn it on,
You may simply uncomment the line “extension=php_gd2.dll” in php.ini and restart the PHP extension.
Change:
,extension=php_gd2.dll
To:
extension=php_gd2.dll
You may also have to correct the extension directory setting
from:
extension_dir = “./”
extension_dir = “./extensions”
To (FOR WINDOWS):
extension_dir = “c:/php/extensions”(php 4)
extension_dir = “c:/php/ext”(php 5)
i like your script, but im a newbie
i am trying to get the thumbnail version working
i have the script in a file called thumb.php in a directory called ‘upload’ that contains a jpeg called ‘athumb.jpeg’
i add the function ‘createThumbnail’ to the ‘thumb.php’ script and also the…
$thumb = createThumbnail($img, $imgPath, “-thumb”, 120, 100, 100);
…code
i changed the above code to read…
$thumb = createThumbnail(‘athumb.jpg’, ‘upload’, “-thumb”, 120, 100, 100);
Should this work? because it doesnt for me….what am i doing wrong?
thanks if you can help
djax
sorry…me again
the ‘athumb.jpg’ IS named ‘athumb.jpg’ on the server. i accidently typed ‘athumb.jpeg’ in my comment
….so this is not the problem
u can take the extension by using smth like:
$ext = substr(strrchr($img, ‘.’), 1);
as if u have name.smth.jpg, your script will take .smth as extension
Font Color
I m having problem for memory limit.. even i set the ini_set(‘memory_limit’, ‘50M’);
but getting error…
Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 13056 bytes) in /home/itsoluti/public_html/affinity/include/common/file_library.php on line 314
Hi Norman..
This is a known bug in GD, if yo refer to my new post on using ImageMagick to achive your image manipulation that should fix your problem: http://nodstrum.com/2007/02/06/imagemagick_manipulation/
Jamie
[...] note that the original code was NOT written by SpeakCoding. We taken it from here and improved it. We’re still not completely done, but we would like to offer you guys this in [...]
[...] I have use a similar script from here: Nodstrum Blog Archive Image Manipulation using PHP, which works fine for me. That script also allows one to resize, rotate and do a few other things [...]
artie…
cool site !!!…
Great script. Thanks for doing it. I have only found a problem with the sintax of
$newName = ”. $newNameE[0] .”. $suffix .‘.’. $newNameE[1] .”;
I have replaced it with:
$newName = $newNameE[0] . $suffix . ‘.’ . $newNameE[1];
Martino
Sorry I meant:
$newName = $newNameE[0] . $suffix . ‘.’ . $newNameE[1];
Hi,
I’m a newbee too. Would you like to give me a php code that uses one of your function?
i like it
Very good! I’ve just wrapped this up into a MODx CMS snippet.
Many thanks!
Great Code!!!
It’s Fantastic!
Congratulations!!
01bdc8275148…
01bdc8275148dcea94ce…
just what i was looking for
I don’t know but your script gives me a long list of error when I upload images directly from the digital camera. I tried uploading .JPGs too that doesn’t seem to work. only photoshop processed photos seem to work ???
this is mentioned above, but any way to do the file resize based on one max width or max height dimension?
jamie, you are the man! keep up this good shit~
Thanks, just what I was looking for.
One thing though, it removes all exif data. I was doing a check on the image to find out its orientation the rotating accordingly.
Is there anyway this information can be kept?
Thanks,
James
if i want to used the Create Thumbnail of your model how can i implement during the upload, it will auto create the thumbnails for the images i upload through the form with other details. currently i want to upload 4 pics and i also have to insert the file name only into the database.
thank you very much
hello it is test. WinRAR provides the full RAR and ZIP file support, can decompress CAB, GZIP, ACE and other archive formats.
ygndqozicaukvefiapzojoiutwxqpggychghello
air flights airfair airfare airfare auction
tickets airfares airfares europe
companies airline discount
airline tickets airline
budget flights business class
cheap airfares cheap airline
cheap airline ticket cheap
flights london cheap flights
airfare cheapest airfares cheapest
airline discount fares discount
domestic airfare domestic
flight to flight to orlando
flights to orlando flights to
tickets international airfare
tickets last minute flight last
lowest airfare lowest airfares
flights really cheap airfare
student airfares the cheapest
tickets book flight flights
discounted flights flight fares
flight reservations cheap
dublin flights to prague flights to
search flights cheapflights fares cheap travel
cheap tickets airlines tickets
Hi
Thanks for Sharing,
I was seeking for reducing image size (in KB) for product images uploaded to osCommerce store.
This all above functions are really nice for image manipulating commonly used functions.
I made master class of these functions but there is a no ways to post my class file here but any one need it contact me through my site.
Many Many Thanks to function author and this site owner.
Complete osCommerce Solutions
Thanks for all, THIS SCRIPTS are working well … + hello from France
that looks very handy and cool
thank you for sharing that with us
Michael
Yes really nice script thanks !
Hi,
Image manupulation using PHP is good and it is most important to the freshers who want to make his career in PHP .
Deep from : Software Development Company
Our Services :
• Website Designing
• Web Development
• PHP Development
• ASP.NET Development
• E-Commerce Website
• Website Re-designing and Maintenance
• Banner Designing
• E-Learning
• Domain Services and Maintenance
Thanks
Hey simply great .I go through whole article nice information.Keep it up…
Leave a reply