Created
August 17, 2015 05:54
-
-
Save mgng/3b52b78569ab8672463a to your computer and use it in GitHub Desktop.
画像リサイズおよびexifの削除処理
This file contains hidden or 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 | |
$blob = getResizedImageBlob("./sample.png", 100, 200); | |
file_put_contents("sample_100x200.png", $blob); | |
/** | |
* @param string $img_path 画像ファイルパス | |
* @param string int $w_re リサイズ後の幅 | |
* @param string int $h_re リサイズ後の高さ | |
* @return string | |
*/ | |
function getResizedImageBlob($img_path, $w_re, $h_re) { | |
$image_info = getimagesize($img_path); | |
$w_org = $image_info[0]; | |
$h_org = $image_info[1]; | |
$blob = ""; | |
if ( class_exists( "Imagick" ) ) { | |
$Image = new \Imagick( $img_path ); | |
$Image->stripImage(); | |
$Image->scaleImage($w_re, $h_re); | |
$blob = $Image->getimageblob(); | |
} else { | |
$type = str_replace("image/", "", $image_info["mime"]); | |
$gd = call_user_func("imagecreatefrom{$type}", $img_path); | |
$gd_out = imagecreatetruecolor( $w_re, $h_re ); | |
imagecopyresampled( $gd_out, $gd, 0,0,0,0, $w_re,$h_re, $w_org,$h_org ); | |
ob_start(); | |
call_user_func("image{$type}", $gd_out); | |
$blob = ob_get_contents(); | |
ob_end_clean(); | |
imagedestroy( $gd ); | |
imagedestroy( $gd_out ); | |
} | |
return $blob; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment