XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
imageresizer.php
Go to the documentation of this file.
1 <?php
2 // $Id: imageresizer.php 825 2011-12-09 00:06:11Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
12 {
13  private $file = ''; #Archivo de origen
14  private $filetarget = ''; # Archivo destino
19  public function __construct($file='', $filetarget=''){
20  $this->file = $file;
21  $this->filetarget = $filetarget;
22  }
27  public function setTargetFile($file){
28  $this->filetarget = $file;
29  }
33  public function getTargetFile(){
34  return $this->filetarget;
35  }
40  public function setSourceFile($file){
41  $this->file = $file;
42  }
46  public function getSourceFile(){
47  return $this->file;
48  }
54  private function checkFiles(){
55  if ($this->file==''){
56  $this->addError(_RMS_CF_FILEEMPTY);
57  return false;
58  }
59  if ($this->filetarget==''){
60  $this->addError(_RMS_CF_NOTARGET);
61  return false;
62  }
63  return true;
64  }
74  public function resizeAndCrop($tw,$th,$red=255,$green=255,$blue=255){
75 
76  if (!$this->checkFiles()) return false;
77 
78  if (!file_exists($this->file)){
79  $this->addError(_RMS_CF_FILENOEXISTS);
80  return false;
81  }
82 
83  list($wo, $ho) = getimagesize($this->file);
84  $percent = 0;
85  $height = $ho * $percent; $width = $wo * $percent;
86 
87  $format = $this->getFormat();
88 
89  $image = $this->createImage($format);
90  if ($wo==$tw && $ho==$th) return true;
91 
92  if ($wo < $ho) {
93  $height = ($tw / $wo) * $ho;
94  } else {
95  $width = ($th / $ho) * $wo;
96  }
97 
98  if ($width < $tw){
99  //if the width is smaller than supplied thumbnail size
100  $width = $tw;
101  $height = ($tw/ $wo) * $ho;
102  }
103 
104  if ($height < $th){
105  $height = $th;
106  $width = ($th / $ho) * $wo;
107  }
108 
109  $thumb = imagecreatetruecolor($width , $height);
110  $bgcolor = imagecolorallocate($thumb, $red, $green, $blue);
111  imagefilledrectangle($thumb, 0, 0, $width, $height, $bgcolor);
112  imagealphablending($thumb, true);
113 
114  imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $wo, $ho);
115  $thumb2 = imagecreatetruecolor($tw, $th);
116  // true color for best quality
117  $bgcolor = imagecolorallocate($thumb2, $red, $green, $blue);
118  imagefilledrectangle($thumb2, 0, 0, $tw , $th , $bgcolor);
119  imagealphablending($thumb2, true);
120 
121  $w1 =($width/2) - ($tw/2);
122  $h1 = ($height/2) - ($th/2);
123 
124  imagecopyresampled($thumb2, $thumb, 0,0, $w1, $h1, $tw, $th,$tw, $th);
125 
126  return $this->imageFromFormat($format, $thumb2);
127  }
133  public function resizeWidth($width, $force = false){
134  if (!$this->checkFiles()) return false;
135 
136  $datos = getimagesize($this->file);
137  $ratio = ($datos[0] / $width);
138  $height = round($datos[1] / $ratio);
139 
140  if (!$force){
141  if ($width >= $datos[0]){
142  if ($this->file != $this->filetarget){
143  copy($this->file, $this->filetarget);
144  }
145  return true;
146  }
147  }
148 
149  $thumb = imagecreatetruecolor($width,$height);
150  $bgcolor = imagecolorallocate($thumb, 255,255,255);
151  imagefilledrectangle($thumb, 0, 0, $width, $height, $bgcolor);
152  imagealphablending($thumb, true);
153  $format = $this->getFormat();
154  $image = $this->createImage($format);
155  imagecopyresampled ($thumb, $image, 0, 0, 0, 0, $width, $height, $datos[0], $datos[1]);
156  return $this->imageFromFormat($format, $thumb);
157  }
165  public function resizeWidthOrHeight($width, $height, $force = false){
166 
167  if (!$this->checkFiles()) return false;
168  $datos = getimagesize($this->file);
169  if ($datos[0] >= $datos[1]){
170  $ratio = ($datos[0] / $width);
171  $height = round($datos[1] / $ratio);
172  } else {
173  $ratio = ($datos[1] / $height);
174  $width = round($datos[0] / $ratio);
175  }
176 
177  $thumb = imagecreatetruecolor($width,$height);
178  $format = $this->getFormat();
179  $image = $this->createImage($format);
180  imagecopyresampled ($thumb, $image, 0, 0, 0, 0, $width, $height, $datos[0], $datos[1]);
181  return $this->imageFromFormat($format, $thumb);
182 
183  }
188  public function createImage($format){
189  switch($format){
190  case 'image/jpeg':
191  return imagecreatefromjpeg($this->file);
192  break;
193  case 'image/gif';
194  return imagecreatefromgif($this->file);
195  break;
196  case 'image/png':
197  return imagecreatefrompng($this->file);
198  break;
199  }
200  }
204  public function imageFromFormat($format, $image, $quality=90){
205  switch ($format){
206  case 'image/jpeg':
207  return imagejpeg($image, $this->filetarget, $quality);
208  break;
209  case 'image/gif';
210  return imagegif($image, $this->filetarget);
211  break;
212  case 'image/png':
213  return imagepng($image, $this->filetarget);
214  break;
215  }
216  }
221  private function getFormat(){
222  if(preg_match("/.jpg/i", $this->file)){
223  $format = 'image/jpeg';
224  } elseif (preg_match("/.gif/i", $this->file)){
225  $format = 'image/gif';
226  } elseif (preg_match("/.png/i", $this->file)){
227  $format = 'image/png';
228  }
229 
230  return $format;
231  }
232 }