Skip to content

Instantly share code, notes, and snippets.

@k4zuki02h4t4
Created June 22, 2018 07:55
Show Gist options
  • Save k4zuki02h4t4/58c02608ee3a6e7c2cc6031122917f5c to your computer and use it in GitHub Desktop.
Save k4zuki02h4t4/58c02608ee3a6e7c2cc6031122917f5c to your computer and use it in GitHub Desktop.
[PHP] GD または Imagick で画像をリサイズする
<?php
/**
* 内接サイズを計算する
*
* @see https://qiita.com/suin/items/b01eebc05209dba0eb3e
*
* @param int $width
* @param int $height
* @param int $containerWidth
* @param int $containerHeight
*
* @return array
*/
function get_contain_size( $width, $height, $containerWidth, $containerHeight ) {
$ratio = $width / $height;
$containerRatio = $containerWidth / $containerHeight;
if ( $ratio > $containerRatio ) {
$result = array( $containerWidth, intval( $containerWidth / $ratio ) );
} else {
$result = array( intval( $containerHeight * $ratio ), $containerHeight );
}
return $result;
}
/**
* リサイズ画像を作成する
*
* @param array $file
* @param array $size
*
* @return bool
*/
function make_thumbnail( $file = array(), $size = array() ) {
$library = determination_image_library();
$result = false;
if ( $library === 'gd' ) {
$result = resize_image_using_gd( $file, $size );
} elseif ( $library === 'imagick' ) {
$result = resize_image_using_imagick( $file, $size );
}
return $result;
}
/**
* 画像処理ライブラリの判定
* 判定は GD が優先される
*
* @return null|string
*/
function determination_image_library () {
$result = null;
if ( extension_loaded( 'gd' ) && function_exists( 'gd_info' ) ) {
$result = 'gd';
} elseif ( extension_loaded( 'imagick' ) && class_exists( 'Imagick' ) ) {
$result = 'imagick';
}
return $result;
}
/**
* GD ライブラリを使って画像をリサイズする
*
* @param array $file
* @param array $size
*
* @return bool
*/
function resize_image_using_gd( $file = array(), $size = array() ) {
$result = false;
switch ( $file['type'] ) {
case IMAGETYPE_JPEG:
$source = imagecreatefromjpeg( $file['src'] );
break;
case IMAGETYPE_PNG:
$source = imagecreatefrompng( $file['src'] );
break;
case IMAGETYPE_GIF:
$source = imagecreatefromgif( $file['src'] );
break;
default:
$source = null;
}
if ( ! is_null( $source ) ) {
$canvas = imagecreatetruecolor( $size['width'], $size['height'] );
imagecopyresampled( $canvas, $source, 0, 0, 0, 0, $size['width'], $size['height'], $size['org_width'], $size['org_height'] );
imagejpeg( $canvas, $file['dest'] );
imagedestroy( $source );
imagedestroy( $canvas );
$result = true;
}
return $result;
}
/**
* Imagick ライブラリを使って画像をリサイズする
*
* @param array $file
* @param array $size
*
* @return bool
*/
function resize_image_using_imagick( $file = array(), $size = array() ) {
$result = false;
switch ( $file['type'] ) {
case IMAGETYPE_JPEG:
$ext = 'JPG';
break;
case IMAGETYPE_PNG:
$ext = 'PNG';
break;
case IMAGETYPE_GIF:
$ext = 'GIF';
break;
default:
$ext = null;
}
if ( ! is_null( $ext ) ) {
$image = new Imagick( $file['src'] );
$image->stripImage();
$image->scaleImage( $size['width'], $size['height'] );
$image->setImageFormat( $ext );
$image->setImageCompressionQuality( 90 );
$image->writeImage( $file['dest'] );
$image->destroy();
$result = true;
}
return $result;
}
<?php
list( $org_width, $org_height, $type ) = getimagesize( $src_file );
list( $resize_width, $resize_height ) = get_contain_size( $org_width, $org_height, '480', '270' );
$ext = pathinfo( $src_file, PATHINFO_EXTENSION );
$file = array(
'src' => $src_file,
'dest' => str_replace( ".{$ext}", "", $src_file ) . "-480.{$ext}",
'type' => $type,
);
$size = array(
'width' => $resize_width,
'height' => $resize_height,
'org_width' => $org_width,
'org_height' => $org_height,
);
make_thumbnail( $file, $size );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment