PHP image resizer


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. * File: SimpleImage.php
  4. * Author: Simon Jarvis
  5. * Copyright: 2006 Simon Jarvis
  6. * Date: 08/11/06
  7. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details:
  18. * http://www.gnu.org/licenses/gpl.html
  19. *
  20. */
  21.  
  22. class SimpleImage {
  23.  
  24. var $image;
  25. var $image_type;
  26.  
  27. function load($filename) {
  28. $image_info = getimagesize($filename);
  29. $this->image_type = $image_info[2];
  30. if( $this->image_type == IMAGETYPE_JPEG ) {
  31. $this->image = imagecreatefromjpeg($filename);
  32. } elseif( $this->image_type == IMAGETYPE_GIF ) {
  33. $this->image = imagecreatefromgif($filename);
  34. } elseif( $this->image_type == IMAGETYPE_PNG ) {
  35. $this->image = imagecreatefrompng($filename);
  36. }
  37. }
  38. function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  39. if( $image_type == IMAGETYPE_JPEG ) {
  40. imagejpeg($this->image,$filename,$compression);
  41. } elseif( $image_type == IMAGETYPE_GIF ) {
  42. imagegif($this->image,$filename);
  43. } elseif( $image_type == IMAGETYPE_PNG ) {
  44. imagepng($this->image,$filename);
  45. }
  46. if( $permissions != null) {
  47. chmod($filename,$permissions);
  48. }
  49. }
  50. function output($image_type=IMAGETYPE_JPEG) {
  51. if( $image_type == IMAGETYPE_JPEG ) {
  52. imagejpeg($this->image);
  53. } elseif( $image_type == IMAGETYPE_GIF ) {
  54. imagegif($this->image);
  55. } elseif( $image_type == IMAGETYPE_PNG ) {
  56. imagepng($this->image);
  57. }
  58. }
  59. function getWidth() {
  60. return imagesx($this->image);
  61. }
  62. function getHeight() {
  63. return imagesy($this->image);
  64. }
  65. function resizeToHeight($height) {
  66. $ratio = $height / $this->getHeight();
  67. $width = $this->getWidth() * $ratio;
  68. $this->resize($width,$height);
  69. }
  70. function resizeToWidth($width) {
  71. $ratio = $width / $this->getWidth();
  72. $height = $this->getheight() * $ratio;
  73. $this->resize($width,$height);
  74. }
  75. function scale($scale) {
  76. $width = $this->getWidth() * $scale/100;
  77. $height = $this->getheight() * $scale/100;
  78. $this->resize($width,$height);
  79. }
  80. function resize($width,$height) {
  81. $new_image = imagecreatetruecolor($width, $height);
  82. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  83. $this->image = $new_image;
  84. }
  85. }
  86. ?>
  87.  
  88.  
  89. Save the above file as SimpleImage.php and take a look at the following examples of how to use the script.
  90.  
  91. The first example below will load a file named picture.jpg resize it to 250 pixels wide and 400 pixels high and resave it as picture2.jpg
  92.  
  93. <?php
  94. include('SimpleImage.php');
  95. $image = new SimpleImage();
  96. $image->load('picture.jpg');
  97. $image->resize(250,400);
  98. $image->save('picture2.jpg');
  99. ?>
  100. If you want to resize to a specifed width but keep the dimensions ratio the same then the script can work out the required height for you, just use the resizeToWidth function.
  101.  
  102. <?php
  103. include('SimpleImage.php');
  104. $image = new SimpleImage();
  105. $image->load('picture.jpg');
  106. $image->resizeToWidth(250);
  107. $image->save('picture2.jpg');
  108. ?>
  109. You may wish to scale an image to a specified percentage like the following which will resize the image to 50% of its original width and height
  110.  
  111. <?php
  112. include('SimpleImage.php');
  113. $image = new SimpleImage();
  114. $image->load('picture.jpg');
  115. $image->scale(50);
  116. $image->save('picture2.jpg');
  117. ?>
  118. You can of course do more than one thing at once. The following example will create two new images with heights of 200 pixels and 500 pixels
  119.  
  120. <?php
  121. include('SimpleImage.php');
  122. $image = new SimpleImage();
  123. $image->load('picture.jpg');
  124. $image->resizeToHeight(500);
  125. $image->save('picture2.jpg');
  126. $image->resizeToHeight(200);
  127. $image->save('picture3.jpg');
  128. ?>
  129. The output function lets you output the image straight to the browser without having to save the file. Its useful for on the fly thumbnail generation
  130.  
  131. <?php
  132. header('Content-Type: image/jpeg');
  133. include('SimpleImage.php');
  134. $image = new SimpleImage();
  135. $image->load('picture.jpg');
  136. $image->resizeToWidth(150);
  137. $image->output();
  138. ?>
  139. The following example will resize and save an image which has been uploaded via a form
  140.  
  141. <?php
  142. if( isset($_POST['submit']) ) {
  143. include('SimpleImage.php');
  144. $image = new SimpleImage();
  145. $image->load($_FILES['uploaded_image']['tmp_name']);
  146. $image->resizeToWidth(150);
  147. $image->output();
  148. } else {
  149. ?>
  150. <form action="upload.php" method="post" enctype="multipart/form-data">
  151. <input type="file" name="uploaded_image" />
  152. <input type="submit" name="submit" value="Upload" />
  153. </form>
  154. <?php
  155. }
  156. ?>

URL: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.