public static function resizeImage($origin, $w, $h, $_options = array()) { $options = array_merge(array('resize' => self::PROPORTIONAL, 'destination' => $origin, 'action' => self::MOVE_ORIG_DEST, 'outputFormat' => self::SAME_AS_ORIGIN), $_options); if (!file_exists($origin)) { return false; } // read file $image_info = getimagesize($origin); $image_type = $image_info[2]; if ($image_info[0] > self::UPLOAD_MAX_WIDTH || $image_info[1] > self::UPLOAD_MAX_HEIGHT) { return "Exceeded maximum dimensions (" . self::UPLOAD_MAX_WIDTH . "x" . self::UPLOAD_MAX_HEIGHT . ")"; } if ($image_type == IMAGETYPE_JPEG) { $image = imagecreatefromjpeg($origin); $extOrig = 'jpg'; } elseif ($image_type == IMAGETYPE_GIF) { $image = imagecreatefromgif($origin); $extOrig = 'gif'; } elseif ($image_type == IMAGETYPE_PNG) { $image = imagecreatefrompng($origin); $extOrig = 'png'; } // destination file extension calculation $extDest = $options['outputFormat'] === self::SAME_AS_ORIGIN ? $extOrig : ImageTools::getExtension($options['destination']); // redimensionar $wimg = imagesx($image); $himg = imagesy($image); $resizeMode = $options['resize']; switch ($resizeMode) { case self::TO_HEIGHT: $ratio = $h / $himg; $height = $h; $width = $wimg * $ratio; break; case self::TO_WIDTH: $ratio = $w / $wimg; $width = $w; $height = $himg * $ratio; break; case self::EXACT_SIZE: $width = $w; $height = $h; break; case self::PROPORTIONAL: default: $ratioh = $h / $himg; $ratiow = $w / $wimg; $ratio = $ratioh > $ratiow ? $ratiow : $ratioh; $width = $wimg * $ratio; $height = $himg * $ratio; break; } // process $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $wimg, $himg); imagedestroy($image); $destination = $options['destination']; if ($extDest == 'jpg' || $extDest == 'jpeg') { imagejpeg($new_image, $destination); } elseif ($extDest == 'gif') { imagegif($new_image, $destination); } elseif ($extDest == 'png') { imagepng($new_image, $destination); } chmod($destination, 0777); if ($options['action'] == self::MOVE_ORIG_DEST) { @unlink($origin); } imagedestroy($new_image); return true; }