Skip to content

Instantly share code, notes, and snippets.

@jedisct1
Created October 4, 2012 03:10
Show Gist options
  • Save jedisct1/3831243 to your computer and use it in GitHub Desktop.
Save jedisct1/3831243 to your computer and use it in GitHub Desktop.
Use with
<?php
namespace Spot;
define('USE_ADAPTATIVE_CROP', FALSE);
define('JUST_DISTORT_AND_FORGET', TRUE);
use \Spot\ImageAutoCrop;
class MediaUtils {
static function get_new_size_with_ratio($owidth, $oheight, $mwidth, $mheight) {
$nwidth = $mwidth;
$nheight = $mheight;
if ($nwidth > $mwidth || $nheight > $mheight) {
$nheight = round($oheight * $mwidth / $owidth);
if ($nheight > $mheight) {
$nheight = $mheight;
$nwidth = round($owidth * $mheight / $oheight);
}
}
return array('nwidth' => $nwidth, 'nheight' => $nheight);
}
static function get_resized_image_from_image(&$img, $mwidth, $mheight, $overscale, $crop) {
require_once APP_LIB_DIR . '/class.picture_storage.inc.php';
if (empty($img)) {
return NULL;
}
if (USE_ADAPTATIVE_CROP === FALSE || $crop === FALSE) {
imageFastAutoCrop($img);
} else {
require_once APP_LIB_DIR . '/class.image_auto_crop.inc.php';
$ci = ImageAutoCrop::autocrop($img);
if (!empty($ci)) {
imageCrop($img,
$ci['offsetx'], $ci['offsety'],
$ci['width'], $ci['height']);
}
unset($ci);
}
$width = imageWidth($img);
$height = imageHeight($img);
if (empty($width) || empty($height)) {
return NULL;
}
if (JUST_DISTORT_AND_FORGET === TRUE && $crop === TRUE) {
$zr1 = $width / $height;
$zr2 = $mwidth / $mheight;
if ($zr1 >= 1.0) {
$cheight = $height;
$cwidth = (int) round($height * $zr2);
} else {
$cwidth = $width;
$cheight = (int) round($width * $zr2);
}
if ($cheight !== $height || $cwidth !== $width) {
$offsetx = (int) round(($width - $cwidth) / 2.0);
$offsety = (int) round(($height - $cheight) / 2.0);
if ($offsetx >= 0 && $offsety >= 0 &&
$offsetx + $cwidth <= $width && $offsety + $cheight <= $height) {
imageCrop($img, $offsetx, $offsety, $cwidth, $cheight);
}
$width = $cwidth;
$height = $cheight;
}
}
$new_size = self::get_new_size_with_ratio($width, $height, $mwidth, $mheight);
$ratio1 = (double) ($new_size['nheight'] / $height);
$ratio2 = (double) ($new_size['nwidth'] / $width);
$ratio = min($ratio1, $ratio2);
imageScale($img, $ratio);
return encodeImage($img, IMAGES_CODEC, IMAGES_CODEC_QUALITY);
}
static function get_resized_image_from_file_name($file_name, $mwidth, $mheight, $overscale, $crop) {
$img = newImage();
decodeImageFile($img, $file_name);
$data = self::get_resized_image_from_image($img, $mwidth, $mheight, $overscale, $crop);
deleteImage($img);
return $data;
}
static function get_resized_image_and_image_object_from_file_name($file_name, $mwidth, $mheight, $overscale, $crop) {
$img = newImage();
decodeImageFile($img, $file_name);
$data = self::get_resized_image_from_image($img, $mwidth, $mheight, $overscale, $crop);
return array('resized_image' => $data, 'image_object' => $img);
}
static function save_image_and_thumbnail($image_tmp_file_name,
$mwidth, $mheight,
$thumb_mwidth, $thumb_mheight, $crop) {
require_once APP_LIB_DIR . '/class.picture_storage.inc.php';
$hash = @md5_file($image_tmp_file_name);
if (empty($hash)) {
throw new \Exception('Nonexistent temporary file');
}
$image_id = $hash . '.' . IMAGES_FILE_EXTENSION;
$resized_image_and_image_object =
self::get_resized_image_and_image_object_from_file_name($image_tmp_file_name,
$mwidth, $mheight, FALSE, FALSE);
$resized_image = $resized_image_and_image_object['resized_image'];
if (empty($resized_image)) {
return NULL;
}
$save_image_with_image_id_ret =
PictureStorage::save_image_with_image_id($image_id, $resized_image);
if ($save_image_with_image_id_ret === SAVE_IMAGE_WITH_IMAGE_ID_RET_ERROR) {
deleteImage($resized_image_and_image_object['image_object']);
return NULL;
}
$ret = array('image_id' => $image_id);
if ($save_image_with_image_id_ret === SAVE_IMAGE_WITH_IMAGE_ID_RET_DIDEXIST) {
$ret['did_exist'] = TRUE;
} else {
assert($save_image_with_image_id_ret === SAVE_IMAGE_WITH_IMAGE_ID_RET_CREATED);
$ret['did_exist'] = FALSE;
}
if (empty($thumb_mwidth) || empty($thumb_mheight)) {
deleteImage($resized_image_and_image_object['image_object']);
return $ret;
}
unset($resized_image_and_image_object['resized_image']);
unset($resized_image);
$thumb_image_id = $hash . '.thumb.' . IMAGES_FILE_EXTENSION;
$thumb_data =
self::get_resized_image_from_image($resized_image_and_image_object['image_object'],
$thumb_mwidth, $thumb_mheight, TRUE, $crop);
deleteImage($resized_image_and_image_object['image_object']);
unset($resized_image_and_image_object);
if (empty($thumb_data) ||
PictureStorage::save_image_with_image_id($thumb_image_id, $thumb_data)
=== SAVE_IMAGE_WITH_IMAGE_ID_RET_ERROR) {
PictureStorage::release_image_with_image_id($image_id);
return NULL;
}
unset($thumb_data);
return $ret + array('thumb_image_id' => $thumb_image_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment