我正在研究一个通过src获取图像数组并将它们合并为单个平铺图像的类,类似于下图;
唯一的问题是提供给班级的图像没有像上图所示的设定尺寸。
该类的以下状态创建垂直堆叠的无间隙图像列表(如下所示),并自动计算导出图像所需的高度(以$total_height
计算)。
我认为由于图像尺寸的无限变化,最终输出中的间隙是不可避免的,但是我不确定在水平和垂直方向上合并平铺的位置。
class BoardCreator {
private $_img_type;
private $_img_urls = array();
public function __construct($img_export_type, array $img_urls) {
$this->_img_type = $img_export_type; // File format for exported image
$this->_img_urls = $img_urls; // Array of image URLs
}
public function GenerateBoard() {
/*
* Arrays to hydrate with loaded image properties & resources
*/
$images = array(); // Image resources
$width = array(); // Image widths
$height = array(); // Image heights
$total_height = 0; // Total height required for the board
/*
* Load in each image, and store its width & height
*/
for ($i=0; $i < count($this->_img_urls); $i++) {
switch (exif_imagetype($this->_img_urls[$i])) {
case IMAGETYPE_JPEG :
$images[$i] = imagecreatefromjpeg($this->_img_urls[$i]);
break;
case IMAGETYPE_PNG :
$images[$i] = imagecreatefrompng($this->_img_urls[$i]);
break;
case IMAGETYPE_GIF :
$images[$i] = imagecreatefromgif($this->_img_urls[$i]);
break;
// default w/ error required
}
// Store the image's dimensions
list($width[$i], $height[$i]) = getimagesize($this->_img_urls[$i]);
// Add this image's height to the required canvas height
$total_height = $total_height + $height[$i];
}
/*
* Create a new "canvas" image with specified dimensions
*/
$canvas_image = imagecreatetruecolor($width[0], $total_height);
/*
* Copy each image into the "canvas" image generated above
*/
$current_x = 0;
$current_y = 0;
for ($i=0; $i < count($images); $i++) {
imagecopy(
$canvas_image, // destination image
$images[$i], // source image
0, // x co-ordinate of destination
$current_y, // y co-ordinate of destination
0, // x co-ordinate of source
0, // y co-ordinate of source
$width[$i], // source img width
$height[$i] // source img height
);
$current_y = $current_y + $height[$i];
}
/*
* Save the resulting image in the format specified at initiation
*/
switch ($this->_img_type) {
case "jpg" :
$images[$i] = imagejpeg($canvas_image, "../board_exports/test.jpg");
break;
case "png" :
$images[$i] = imagepng($canvas_image, "../board_exports/test.png");
break;
case "gif" :
$images[$i] = imagegif($canvas_image, "../board_exports/test.gif");
break;
default :
// Create an error to handle here
die("Error in BoardCreator.php (Method GenerateBoard() )");
break;
}
/*
* Release the created image from memory
*/
imagedestroy($canvas_image);
/*
* Loop through and release each loaded image
*/
for ($i=0; $i < count($this->_img_urls); $i++) {
imagedestroy($images[$i]);
}
}
答案 0 :(得分:2)
我制作了这段代码来帮助你。
1.-修复“图像尺寸的无限变化”我使用此功能制作图像的缩略图:
function generate_image_thumbnail($source_image_path, $thumbnail_image_path,$THUMBNAIL_IMAGE_MAX_WIDTH,$THUMBNAIL_IMAGE_MAX_HEIGHT)
{
$condicion = GetImageSize($source_image_path); // image format?
if($condicion[2] == 1) //gif
$original = imagecreatefromgif("$source_image_path");
if($condicion[2] == 2) //jpg
$original = imagecreatefromjpeg("$source_image_path");
if($condicion[2] == 3) // png
$original = imagecreatefrompng("$source_image_path");
$thumb = imagecreatetruecolor($THUMBNAIL_IMAGE_MAX_WIDTH,$THUMBNAIL_IMAGE_MAX_HEIGHT);
$ancho = imagesx($original);
$alto = imagesy($original);
imagecopyresampled($thumb,$original,0,0,0,0,$THUMBNAIL_IMAGE_MAX_WIDTH,$THUMBNAIL_IMAGE_MAX_HEIGHT,$ancho,$alto);
imagejpeg($thumb,$thumbnail_image_path,90);
return true;
}
2.-创建图像以粘贴缩略图
$size_image=460;
$img_disp = imagecreatetruecolor($size_image,$size_image);
$backcolor = imagecolorallocate($img_disp,0,0,0);
imagefill($img_disp,0,0,$backcolor);
3.-开始粘贴缩略图
//$THUMBNAIL_IMAGE=150;
$images_by_side = round($size_image/$THUMBNAIL_IMAGE); // round(460/150)=3 3 images by side
$separator = $size_image%$THUMBNAIL_IMAGE; //150*3=450... so 10px in total of space
$space_btwen_images=$separator/($images_by_side+1); //10px / 3 of total images + 1 =2,5
$total_image = pow($images_by_side , 2 ); //total images to paste (images by side)^2.. 3^2=9
//some math calculations to make more nice the output image
$dst_x=$space_btwen_images;
$cont_imgs=0;
for($x=0;$x<$total_image;$x++) {
if($cont_imgs == $images_by_side){
$dst_x=$dst_x+$THUMBNAIL_IMAGE+$space_btwen_images;
$cont_imgs=0;
}
$dst_y=$cont_imgs*$THUMBNAIL_IMAGE+($space_btwen_images * ($cont_imgs+1));
$cont_imgs++;
$thumb_image=$img_tmb_dir.$arr_img[$x]; //image to paste
$condicion = GetImageSize($thumb_image); // image format?
if($condicion[2] == 1) // gif
$image_to_copy = imagecreatefromgif("$thumb_image");
if($condicion[2] == 2) // jpg
$image_to_copy = imagecreatefromjpeg("$thumb_image");
if($condicion[2] == 3) // png
$image_to_copy = imagecreatefrompng("$thumb_image");
echo "dst_x=".$dst_x."; dst_y=".$dst_y.";<br>";
//output to check the destinations of the images to paste
/*
dst_x=2.5; dst_y=2.5;
dst_x=2.5; dst_y=155;
dst_x=2.5; dst_y=307.5;
dst_x=155; dst_y=2.5;
dst_x=155; dst_y=155;
dst_x=155; dst_y=307.5;
dst_x=307.5; dst_y=2.5;
dst_x=307.5; dst_y=155;
dst_x=307.5; dst_y=307.5;
*/
imagecopy($img_disp, $image_to_copy, $dst_x, $dst_y, 0, 0, $THUMBNAIL_IMAGE, $THUMBNAIL_IMAGE);
}
imagejpeg($img_disp, "test3.jpg",90);
imageDestroy($img_disp);
echo '<img src="test3.jpg"/>';
4.-
我的图片(150 x 150)和$ size_image = 460;将返回
和$ size_image = 610将返回