The SimpleImage Class
In a previous tutorial we learned how to upload an image using HTML and PHP. Now we will talk some about the image functions built into PHP, specifically how they can be used to resize and save images.
Let’s say the users on your website can upload their profile picture. You might be able to get by with one image only. Or, you can save 2 or 3 different sized versions of the image, to be used in different parts of your site. For example, you may want a thumbnail of the profile picture to show in a list of users. And you may want a second medium sized image to display on the user’s profile page. Finally, a large version of the image could be displayed when a user clicks the medium sized picture, to give them a close up view.
To accomplish this we’ll use some of PHP’s image functions and wrap them up in nice neat package that we’ll call the SimpleImage class.
Copy and paste the following code into a new PHP file.
<?php class SimpleImage { var $image; var $image_type; function isValidImage() { if (imagesx($this->image) !== false) { return true; } else { return false; } } function load($filename) { $image_info = getimagesize($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); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } ?>
Now we need to call the methods of our class to perform some resizing. Copy and paste the following code below your class:
// The directory where our images will be stored $file_directory = "images/users/1/"; // The path to the original image file on our web server $original_file_path = $file_directory . "profile.jpg"; // Save a large version of the image that is no wider and no taller than 600 pixels $image = new SimpleImage(); $image->load($original_file_path); if($image->getWidth() > 600){ $image->resizeToWidth(600); } if($image->getHeight() > 600){ $image->resizeToHeight(600); } $image->save($file_directory."profile_600x600.jpg"); // Create a medium sized version no wider and no taller than 300 pixels $image = new SimpleImage(); $image->load($original_file_path); if($image->getWidth() > 300){ $image->resizeToWidth(300); } if($image->getHeight() > 300){ $image->resizeToHeight(300); } $image->save($file_directory."profile_300x300.jpg"); // Create a thumbnail version no wider and no taller than 60 pixels $image = new SimpleImage(); $image->load($original_file_path); if($image->getWidth() > 60){ $image->resizeToWidth(60); } if($image->getHeight() > 60){ $image->resizeToHeight(60); } $image->save($file_directory."profile_60x60.jpg");
When our file is executed it will save 3 differently sized images, small, medium, and large, from the one original image.