Skip to content

Instantly share code, notes, and snippets.

@mgng
Created August 17, 2015 05:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgng/3b52b78569ab8672463a to your computer and use it in GitHub Desktop.
Save mgng/3b52b78569ab8672463a to your computer and use it in GitHub Desktop.
画像リサイズおよびexifの削除処理
<?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