Resizing images with


/ 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.  
  90.  
  91.  
  92.  
  93. Save the above file as SimpleImage.php and take a look at the following examples of how to use the script.
  94.  
  95. 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
  96.  
  97. <?php
  98. include('SimpleImage.php');
  99. $image = new SimpleImage();
  100. $image->load('picture.jpg');
  101. $image->resize(250,400);
  102. $image->save('picture2.jpg');
  103. ?>
  104.  
  105. 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.
  106.  
  107. <?php
  108. include('SimpleImage.php');
  109. $image = new SimpleImage();
  110. $image->load('picture.jpg');
  111. $image->resizeToWidth(250);
  112. $image->save('picture2.jpg');
  113. ?>
  114.  
  115. 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
  116.  
  117. <?php
  118. include('SimpleImage.php');
  119. $image = new SimpleImage();
  120. $image->load('picture.jpg');
  121. $image->scale(50);
  122. $image->save('picture2.jpg');
  123. ?>
  124.  
  125. 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
  126.  
  127. <?php
  128. include('SimpleImage.php');
  129. $image = new SimpleImage();
  130. $image->load('picture.jpg');
  131. $image->resizeToHeight(500);
  132. $image->save('picture2.jpg');
  133. $image->resizeToHeight(200);
  134. $image->save('picture3.jpg');
  135. ?>
  136.  
  137. 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
  138.  
  139. <?php
  140. header('Content-Type: image/jpeg');
  141. include('SimpleImage.php');
  142. $image = new SimpleImage();
  143. $image->load('picture.jpg');
  144. $image->resizeToWidth(150);
  145. $image->output();
  146. ?>
  147.  
  148. The following example will resize and save an image which has been uploaded via a form
  149.  
  150. <?php
  151. if( isset($_POST['submit']) ) {
  152. include('SimpleImage.php');
  153. $image = new SimpleImage();
  154. $image->load($_FILES['uploaded_image']['tmp_name']);
  155. $image->resizeToWidth(150);
  156. $image->output();
  157. } else {
  158. ?>
  159.  
  160. <form action="upload.php" method="post" enctype="multipart/form-data">
  161. <input type="file" name="uploaded_image" />
  162. <input type="submit" name="submit" value="Upload" />
  163. </form>
  164.  
  165. <?php
  166. }
  167. ?>

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.