Resizing images with PHP


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

The following script will easily allow you to resize images using PHP.


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.  
  29. $image_info = getimagesize($filename);
  30. $this->image_type = $image_info[2];
  31. if( $this->image_type == IMAGETYPE_JPEG ) {
  32. $this->image = imagecreatefromjpeg($filename);
  33. } elseif( $this->image_type == IMAGETYPE_GIF ) {
  34. $this->image = imagecreatefromgif($filename);
  35. } elseif( $this->image_type == IMAGETYPE_PNG ) {
  36. $this->image = imagecreatefrompng($filename);
  37. }
  38. }
  39. function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  40. if( $image_type == IMAGETYPE_JPEG ) {
  41. imagejpeg($this->image,$filename,$compression);
  42. } elseif( $image_type == IMAGETYPE_GIF ) {
  43. imagegif($this->image,$filename);
  44. } elseif( $image_type == IMAGETYPE_PNG ) {
  45. imagepng($this->image,$filename);
  46. }
  47. if( $permissions != null) {
  48. chmod($filename,$permissions);
  49. }
  50. }
  51. function output($image_type=IMAGETYPE_JPEG) {
  52. if( $image_type == IMAGETYPE_JPEG ) {
  53. imagejpeg($this->image);
  54. } elseif( $image_type == IMAGETYPE_GIF ) {
  55. imagegif($this->image);
  56. } elseif( $image_type == IMAGETYPE_PNG ) {
  57. imagepng($this->image);
  58. }
  59. }
  60. function getWidth() {
  61. return imagesx($this->image);
  62. }
  63. function getHeight() {
  64. return imagesy($this->image);
  65. }
  66. function resizeToHeight($height) {
  67. $ratio = $height / $this->getHeight();
  68. $width = $this->getWidth() * $ratio;
  69. $this->resize($width,$height);
  70. }
  71. function resizeToWidth($width) {
  72. $ratio = $width / $this->getWidth();
  73. $height = $this->getheight() * $ratio;
  74. $this->resize($width,$height);
  75. }
  76. function scale($scale) {
  77. $width = $this->getWidth() * $scale/100;
  78. $height = $this->getheight() * $scale/100;
  79. $this->resize($width,$height);
  80. }
  81. function resize($width,$height) {
  82. $new_image = imagecreatetruecolor($width, $height);
  83. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  84. $this->image = $new_image;
  85. }
  86. }
  87. ?>

URL: http://cristea.me/resizing-images-with-php/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.