Created
July 10, 2014 09:41
-
-
Save polikeiji/c82e0cb021394dd8432a to your computer and use it in GitHub Desktop.
サムネイル作成用のPHPクラス。GDベース。サムネイル画像をバイナリーデータとして返してくれる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ThumbnailUtility { | |
const TYPE_KEEP_RATIO = 0; | |
const TYPE_TRIM = 1; | |
public static function Resize( | |
$file_path_or_url, | |
$thumbnail_width = null, | |
$thumbnail_height = null, | |
$type = ThumbnailUtility::TYPE_KEEP_RATIO) { | |
if (($original_image_source = @imagecreatefromstring(file_get_contents($file_path_or_url))) === false) { | |
throw new ThumbnailUtilityException('failed in load image data'); | |
} | |
if (($original_image_size = @getimagesize($file_path_or_url)) === false) { | |
throw new ThumbnailUtilityException('failed get image size'); | |
} | |
$original_image_width = $original_image_size[0]; | |
$original_image_height = $original_image_size[1]; | |
switch ($type) { | |
case ThumbnailUtility::TYPE_KEEP_RATIO: | |
if ($thumbnail_width != null) { | |
$thumbnail_height = intval($original_image_height * $thumbnail_width / $original_image_width); | |
} | |
else if ($thumbnail_height != null) { | |
$thumbnail_width = intval($original_image_width * $thumbnail_height / $original_image_height); | |
} | |
else { | |
throw new ThumbnailUtilityException('need width or height for keep ratio resizing'); | |
} | |
if (($thumbnail_source = @imagecreatetruecolor($thumbnail_width, $thumbnail_height)) === false) { | |
throw new ThumbnailUtilityException('failed to create new image'); | |
} | |
if ($original_image_size[2] == IMAGETYPE_PNG) { | |
if (@imagecolortransparent($thumbnail_source) === false) { | |
throw new ThumbnailUtilityException(); | |
} | |
if (@imagealphablending($thumbnail_source, false) === false) { | |
throw new ThumbnailUtilityException(); | |
} | |
if (($color = @imagecolorallocatealpha($thumbnail_source, 0, 0, 0, 127)) === false || | |
@imagefill($thumbnail_source, 0, 0, $color) === false) { | |
throw new ThumbnailUtilityException(); | |
} | |
if (@imagesavealpha($thumbnail_source, true) === false) { | |
throw new ThumbnailUtilityException(); | |
} | |
} | |
if (@imagecopyresampled($thumbnail_source, $original_image_source, 0, 0, 0, 0, | |
$thumbnail_width, $thumbnail_height, $original_image_width, | |
$original_image_height) === false) { | |
throw new ThumbnailUtilityException('failed in imagecopyresized'); | |
} | |
break; | |
case ThumbnailUtility::TYPE_TRIM: | |
if ($thumbnail_width == null || $thumbnail_height == null) { | |
throw new ThumbnailUtilityException('need width and height for trim resizing'); | |
} | |
if (($thumbnail_source = @imagecreatetruecolor($thumbnail_width, $thumbnail_height)) === false) { | |
throw new ThumbnailUtilityException('failed to create new image'); | |
} | |
$thumbnail_ratio = $thumbnail_width / $thumbnail_height; | |
$desired_width = $original_image_height * $thumbnail_ratio; | |
if ($desired_width < $original_image_width) { | |
$cut_width = $original_image_width - $desired_width; | |
$cut_height = 0; | |
} | |
else if ($desired_width > $original_image_width) { | |
$cut_width = 0; | |
$cut_height = $original_image_height - $original_image_width / $thumbnail_ratio; | |
} | |
if (@imagecopyresampled($thumbnail_source, $original_image_source, | |
0, 0, $cut_width * 0.5, $cut_height * 0.5, | |
$thumbnail_width, $thumbnail_height, $original_image_width - $cut_width, | |
$original_image_height - $cut_height) === false) { | |
throw new ThumbnailUtilityException('failed in imagecopyresized'); | |
} | |
break; | |
default: | |
throw new ThumbnailUtilityException('invalid generating thumbnail type'); | |
} | |
if (($thumbnail_information = @getimagesize($file_path_or_url)) === false) { | |
throw new ThumbnailUtilityException('cannot get image type'); | |
} | |
ob_start(); | |
switch ($thumbnail_information[2]) { | |
case IMAGETYPE_JPEG: | |
case IMAGETYPE_JPEG2000: | |
if (@imagejpeg($thumbnail_source, null, 100) === false) { | |
throw new ThumbnailUtilityException('failed to get string of thumbnail'); | |
} | |
break; | |
case IMAGETYPE_GIF: | |
if (@imagegif($thumbnail_source) === false) { | |
throw new ThumbnailUtilityException('failed to get string of thumbnail'); | |
} | |
break; | |
case IMAGETYPE_PNG: | |
if (@imagepng($thumbnail_source, null, 0) === false) { | |
throw new ThumbnailUtilityException('failed to get string of thumbnail'); | |
} | |
break; | |
default: | |
throw new ThumbnailUtilityException('unsupported image type'); | |
} | |
imagedestroy($original_image_source); | |
imagedestroy($thumbnail_source); | |
return ob_get_clean(); | |
} | |
} | |
class ThumbnailUtilityException extends Exception { | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
S3にサムネイルをアップするときに、サーバー上に一時ファイルを作りたく無かったから作った。