You found some cool royalty-free pictures online, that really want to add to your site. What would you do? Normally, you would download them locally, fire up your favorite FTP client, connect to your server, navigate to the right folders, and upload them.
With this simple php library, you can now do this programmaticaly. All you need to have is the GD library enabled, which every modern host has nowadays.
The library takes the picture's URL as input, and loads the image. Then you can resize it, scale it, save it to disk, or simply output it to your browser. What you do with it, you decide.
Here is the code:
<?php /* * File: SimpleImage.php * Author: Chris Michaelides * Copyright: 2014 Chris Michaelides * Date: 20/02/14 * Link: http://www.chrismichaelides.eu * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details: * http://www.gnu.org/licenses/gpl.html * */ class SimpleImage { var $image; var $image_type; function getimagesize_remote($image_url) { $gis = array(); $ext = pathinfo($image_url,PATHINFO_EXTENSION); switch ($ext) { case 'jpg': case 'JPG': $im = imagecreatefromjpeg($image_url); $gis[2] = IMAGETYPE_JPEG; break; case 'gif': case 'GIF': $im = imagecreatefromgif($image_url); $gis[2] = IMAGETYPE_GIF; break; case 'png': case 'PNG': $im = imagecreatefrompng($image_url); $gis[2] = IMAGETYPE_PNG; break; } if (!$im) { return false; } $gis[0] = ImageSX($im); $gis[1] = ImageSY($im); // array member 3 is used below to keep with current getimagesize standards $gis[3] = "width={$gis[0]} height={$gis[1]}"; ImageDestroy($im); return $gis; } /** * This for future use */ /* function validateImage($image) { $mime = array('image/gif' => 'gif', 'image/jpeg' => 'jpeg', 'image/png' => 'png', 'application/x-shockwave-flash' => 'swf', 'image/psd' => 'psd', 'image/bmp' => 'bmp', 'image/tiff' => 'tiff', 'image/jp2' => 'jp2', 'image/iff' => 'iff', 'image/vnd.wap.wbmp' => 'bmp', 'image/xbm' => 'xbm', 'image/vnd.microsoft.icon' => 'ico'); // Get File Extension (if any) $ext = strtolower(substr(strrchr($image, "."), 1)); // Check for a correct extension. The image file hasn't an extension? Add one $file_info = $this->getimagesize_remote($image); if(empty($file_info)) // No Image? { return false; } else // An Image? { $file_mime = $file_info['mime']; //print_r($file_mime); if($file_mime == 'image/jpeg' || $file_mime == 'image/png' || $file_mime == 'image/vnd.microsoft.icon' || $file_mime == 'image/tif' || $file_mime == 'image/bmp' || $file_mime == 'image/gif') { return true; } return false; } } */ function load($filename) { $image_info = $this->getimagesize_remote($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = @imagecreatetruecolor($width, $height) or $new_image = imagecreate($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } function close() { ImageDestroy($this->image); } } ?>
The library is based of Simon Jarvis' original SimpleImage library. Follow the above link to see some examples of usage, and an updated resize method, which preserves gif and png transparency.
One thing that Simon didn't think about was a method to remove the image from memory when done processing, to save resources. I've added a close method, which will destroy the image from memory, when called.