Create combine thumbnail from multiple images. Examples http://planet-green.com/archives/446
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 | |
/* | |
* combine_thumb.php | |
* | |
* examples - http://planet-green.com/archives/446 | |
* | |
* System Requirements: | |
* PHP v5.3+ | |
* ImageMagick | |
* | |
* Copyright (c) 2017 tomoya kawabata. | |
* http://planet-green.com/ | |
* | |
* Released under the MIT license | |
* (See copy at http://opensource.org/licenses/mit-license.php) | |
*/ | |
class combine_thumb | |
{ | |
static function build_combine_thumb(Array $filePaths, $size_w, $size_h , $dest_jpg_path, $quality=70) | |
{ | |
$img_dest = new Imagick(); | |
if ( !$img_dest ) | |
{ | |
throw new Exception("Can't create new image."); | |
} | |
$img_dest->newImage($size_w, $size_h, new ImagickPixel('rgb(200,200,200)')); | |
$img_cnt = count($filePaths); | |
switch($img_cnt) | |
{ | |
case 1: | |
$_size_w = $size_w; | |
$_size_h = $size_h; | |
break; | |
case 2: | |
$_size_w = $size_w; | |
$_size_h = $size_h/2; | |
break; | |
case 3: | |
$_size_w = $size_w / 3; | |
$_size_h = $size_h; | |
break; | |
default: | |
case 4: | |
$_size_w = $size_w / 2; | |
$_size_h = $size_h / 2; | |
break; | |
} | |
$x = $y = 0; | |
foreach ($filePaths as $file) | |
{ | |
$img_tmp = new Imagick($file); | |
if ( !$img_tmp ) | |
{ | |
throw new Exception("Can't open image file."); | |
} | |
$img_tmp->stripImage(); | |
$img_tmp->cropthumbnailimage( round($_size_w)+1, round($_size_h)+1); | |
$img_dest->compositeImage( $img_tmp, imagick::COMPOSITE_DEFAULT, round($x), round($y) ); | |
$img_tmp->clear(); | |
$x += $_size_w; | |
if( $x >= $size_w ) | |
{ | |
$x = 0; | |
$y += $_size_h; | |
} | |
} | |
$img_dest->setImageFormat('jpeg'); | |
$img_dest->setCompression(Imagick::COMPRESSION_JPEG); | |
$img_dest->setImageCompressionQuality($quality); | |
$img_dest->writeImage($dest_jpg_path); | |
$img_dest->clear(); | |
return true; | |
} | |
} |
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 | |
require_once "combine_thumb.php"; | |
$filePaths = array( | |
"/path/to/src/image1.jpg", | |
"/path/to/src/image2.jpg", | |
"/path/to/src/image3.jpg", | |
"/path/to/src/image4.jpg", | |
); | |
$dest_jpg_path = "/path/to/dest/output.jpg"; | |
combine_thumb::build_combine_thumb($filePaths, 80, 80 , $dest_jpg_path, 70); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment