1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| <?php
class ResizeImage { private $image; private $image_type; public function init($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; switch($this->image_type) { case IMAGETYPE_JPEG: $this->image = imagecreatefromjpeg($filename); break; case IMAGETYPE_GIF: $this->image = imagecreatefromgif($filename); break; case IMAGETYPE_PNG: $this->image = imagecreatefrompng($filename); break; } } public function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { switch($image_type) { case IMAGETYPE_JPEG: imagejpeg($this->image, $filename, $compression); break; case IMAGETYPE_GIF: imagegif($this->image,$filename); break; case IMAGETYPE_PNG: imagepng($this->image,$filename); break; default: return false; break; } if($permissions != null) { chmod($filename, $permissions); } return true; }
public function output($image_type = IMAGETYPE_JPEG) { switch($image_type) { case IMAGETYPE_JPEG: imagejpeg($this->image); break; case IMAGETYPE_GIF: imagegif($this->image); break; case IMAGETYPE_PNG: imagepng($this->image); break; } }
public function get_width() { return imagesx($this->image); } public function get_height() { return imagesy($this->image); } public function resize_to_height($height) { $ratio = $height / $this->get_height(); $width = $this->get_width() * $ratio; $this->resize($width, $height); } public function resize_to_width($width) { $ratio = $width / $this->get_width(); $height = $this->get_height() * $ratio; $this->resize($width, $height); } public function scale($scale) { $width = $this->get_width() * $scale/100; $height = $this->get_height() * $scale/100; $this->resize($width, $height); } public function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->get_width(), $this->get_height()); $this->image = $new_image; } } ?>
|