Image Processing with PHP
by Emmanuel Ndayiragije, Sep. 04, 2010 at 09:09 PM
Have you ever asked yourself how you can process images for your website or your images API like Flickr and so on?
I have been using third part software , namely Photoshop, Gimp, Inkscape,... to resize or other image processing capabilities from those software.
Since i ve found the way to manipulate directly my images in PHP with no need of Photoshop.
I am not saying that Photoshop, Gimp,... aren't good. No they are pretty good to work with.
But PHP with gd enabled is also good, you can perfom much with this feature.
I have written a class which helps me to process my images. When I upload any image of any format or size, I don't need to manually check
weither the size or the format are good or not.
My class does all the job me. It takes the source file of the image, determine its size, its format before it resizes it and crop it to the
needed size provided to it.
With this class I can also apply watermark to my pictures and copy to it for instance an copyright image.
It works for me as fine.
In combination with Ajax, i use that class to retrieve an image from my webserver to be displayed on this site. When a next button
or prev button is clicked, ajax tries to connect to the server to get a next or prev. image that you see on my site.
For some websites with a slide show with several tens of true colored picture get a problem of the bandwidth for your site.
Using the technics of ajax, you can solve that problem, because pictures are big in bytes than text.
<?php /** * Description of Image_Processor* * @author Emmanuel_Leonie */ class Image_Processor { public $filename = null; public $watermark_filename = null; public $watermark = null; private $path; private $source = null; private $target = null; private $imageW; private $imageH; private $mime; // private $crop_width; private $crop_height; public function __construct($filename, $crop_width =null, $crop_height =null, $dir =null) { $this->filename = $filename; $this->path = $dir ? $dir : "../FotosUploaded/"; //exit; $this->path = $dir; } $this->watermark_filename = $this->path . "/watermark.png"; $this->crop_width = $crop_width; $this->crop_height = $crop_height; if($this->filename) if($this->watermark_filename) // if(!$img_str) throw new Exception("Sorry, cannot get contents of this file."); // throw new Exception("Sorry, cannot create resource file."); //Not necessary //Get Image size from the resource of the file; if($this->source){ } //Get image Mime type $this->mime = $size['mime']; } public function getImageWidth(){ return $this->imageW; } public function getImageHeight(){ return $this->imageH; } public function getImageType(){ $type = ""; switch($this->mime){ case "image/jpeg": case "image/jpg": case "image/pjpeg": $type = ".jpg"; break; case "image/png"; $type = ".png"; break; case "image/gif": $type = ".gif"; break; default : $type = null; break; } return $type; } public function Resize($Width = 600, $Height = 600) { if(!$Width && !$Height) throw new Exception("You Must provide the cropped width or cropped height"); { if($this->imageW <= $Width && $this->imageH <= $Height) { $NewWidth = $this->imageW; $NewHeight = $this->imageH; } else { $OrigRatio =$this->imageW / $this->imageH; if(($Width/$Height) > $OrigRatio) { $NewWidth = $Height * $OrigRatio; $NewHeight = $Height; } else { $NewHeight = $Width / $OrigRatio; $NewWidth = $Width; } } $NewWidth, $NewHeight, $this->imageW, $this->imageH); return $this->target = $ResampledImage; } else return null; } public function Crop($Width, $Height = 0) { if(!$Width && !$Height) throw new Exception("You Must provide the cropped width or cropped height"); elseif($Width && !$Height) $Height = $Width; $width = $this->getImageWidth(); $height = $this->getImageHeight(); // // $this->target = $imgCropped; if($this->watermark) { $this->Watermark (); } // return $this->target; } public function Watermark() { if(!$this->watermark) return null; //Set the blending mode and save its alpha //Make that width = height if($imageWidth <= $watermarkWidth) { $watermarkWidth = $imageWidth; $x = 0; $y = $imageHeight - $watermarkHeight; } elseif($imageWidth > $watermarkWidth) { $x = $imageWidth - $watermarkWidth; $y = $imageHeight - $watermarkHeight; } // Copy a $watermark to $image $watermarkHeight); // Save the copied $image to the $target_file // end then destroy the $image and $watermark resources return $this->target; } public function Save($asFile = "") { if($this->target){ if($this->mime){ switch ($this->mime){ case "image/jpeg": case "image/jpg": case "image/pjpeg": break; case "image/png": break; case "image/gif": break; default : break; } } } } } ?>
If you want to test for yourself this great class. You need to include this class in your PHP file and instantiate an object of this, before you can call upon the defined methode and its public properties. For this raison, I've synthesized an PHP example so that you can test it and see its perfomance. You can further extend this class and add your own methods in other to enrich it.
Here is code test:
<?php require_once 'Image_Processor.class.php'; $path_to_save_in = "/folder/"; $img_proc = new Image_Processor($_FILES['file']['tmp_name'], 0, 0, $path_to_save_in); $img_proc->getImageWidth(); $img_proc->getImageHeight(); $img_proc->Resize(600, 600); $img_proc->Crop(520,242); $saveAs = "Filename" . $img_proc->getImageType(); $img_proc->Save($saveAs); ?>
Have you any problem using it? Let tell it! Have fun!
For those who like to see demo of the execution of this ImageProcessor, here above defined, you can browse to this link: Imageprocessor
Post Comment|
Permant linksAvailable Comments :
Joannes
Apr. 27, 2011 at 02:16 PM
Why don't you write in french?
Emmanuel Ndayiragije
May. 05, 2011 at 04:30 PM
Salut Joannes,
Quand j'ai programmé ce système, je l'ai codé supportant l'englais seulement. Bientôt, je vais l'étendre pour pouvoir supporter d'autres langages(Internationalization & Localization).
Toute fois, merci pour votre commentaire.
Your Attention! Please do not post message with spam on this blog. We try to be cool to each other and post relevant comments and relating to the appropriated topic or entry. Let's try to get a nice conversation over here.
Entry Categories:
Latest News
- Jun. 20, 2013 at 15:20
Fighting between factions of al-Shabab takes place near the Somali coast, witnesses say - the first reported clashes within the Islamist mil...
Read more» - Jun. 20, 2013 at 15:19
Teacher Jeremy Forrest is found guilty of abducting a pupil from the Eastbourne school he taught at.
Read more» - Jun. 20, 2013 at 15:01
More than one in three women worldwide have experienced physical or sexual violence, a report by the World Health Organization and other gro...
Read more» - Jun. 20, 2013 at 14:55
A Spanish judge summons Barcelona's Argentine footballer Lionel Messi to appear in court in September over tax fraud allegations.
Read more» - Jun. 20, 2013 at 14:46
Watch the latest news summary from BBC World News. International news updated 24 hours a day.
Read more» - Jun. 20, 2013 at 14:35
The cellar where Austrian Josef Fritzl kept his daughter captive for 24 years, and fathered seven children with her, is being cemented in.
Read more» - Jun. 20, 2013 at 14:30
A US search vessel finds wreckage from a plane which had disappeared five years ago with 14 people on board off the Venezuelan coast.
Read more» - Jun. 20, 2013 at 14:12
Dramatic pictures reveal how a blaze has destroyed a firelighter factory in the Netherlands.
Read more» - Jun. 20, 2013 at 14:07
Global stock markets fall sharply after the US central bank signals it could begin to scale back its economic stimulus programme later this ...
Read more» - Jun. 20, 2013 at 14:06
Nine people are taken to hospital after a car hits parents and children as they used a school pelican crossing in a south Wales village.
Read more» - Jun. 20, 2013 at 14:06
England's NHS regulator will name some officials accused of covering up a failure to investigate deaths of babies at a Cumbria hospital, min...
Read more» - Jun. 20, 2013 at 14:05
Psychic Sally Morgan says she has accepted an apology and £125,000 in damages from the Daily Mail after it suggested she "perpetrated a sca...
Read more» - Jun. 20, 2013 at 14:04
Singapore's prime minister warns that the haze engulfing the city could last for weeks, as air pollution in the city-state soared to record ...
Read more» - Jun. 20, 2013 at 14:00
Twenty-four hours of news photos from around the world
Read more» - Jun. 20, 2013 at 13:51
A row over the status of the Taliban's office in Qatar persists as the militant group continues to display its flag despite Afghan protests....
Read more» - Jun. 20, 2013 at 13:20
Read more» - Jun. 20, 2013 at 13:19
Tony Soprano - James Gandolfini's defining role
Read more» - Jun. 20, 2013 at 13:06
The International Criminal Court (ICC) pushes back the trial of Kenyan President Uhuru Kenyatta to 12 November.
Read more» - Jun. 20, 2013 at 13:00
A terrified burglar hands himself to police in New Zealand after breaking into a house and stumbling across a body hanging in the dark.
Read more» - Jun. 20, 2013 at 11:56
Six ancient sites in Syria are added to a UN list of endangered World Heritage sites because of the threat from the conflict there.
Read more»
Recent Comments:
May. 07, 2013 at 10:57 AM
Pleasing to find soemone who can think like that
May. 07, 2013 at 10:51 AM
Thinking like that is really amaizng
Oct. 27, 2012 at 03:57 PM
That framework (Kazinduzi) is right good, & simple to use, i download it and set it on my ubuntu box, it just works fine. good work, man.
Oct. 27, 2012 at 03:56 PM
Great framework, it works fine on my server.
Jun. 29, 2012 at 05:27 PM
Reaction to "Cheap online prescription glasses".
Thanks for your appreciation to my blog.
Of course this blog is about to publish my own expe... Read more»
Archives:
- Playing video and audio with HTML5 July 23, 2010
- Aggregating objects within PHP5 June 26, 2010
- Creating a login system in your website May 05, 2010
- How to Setup your own Webserver May 05, 2010
- Welcome Entry May 05, 2010





