使用PHP脚本将多个图像合并为一个 [英] Joining Multiple Images into one with a PHP script

查看:69
本文介绍了使用PHP脚本将多个图像合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此测试页 http://thechozenfew.net/projects/write_font.php会在输入框中将文本生成为字体.每个字母是一张不同的图片,我该如何使用php脚本将所有这些图片合并为一张图片?

I have this test page http://thechozenfew.net/projects/write_font.php that generates the text in the input box as a font. Each letter is a different picture, How would i join all these pictures into one image with a php script?

这是我到目前为止尝试过的,但是图像彼此重叠.

This what i've tried so far but the images appear on top of each other.

Header ("Content-type: image/gif"); 

$image1Url = "../images/avatar.png";
$image2Url = "../images/key.png";
$image3Url = "../images/safari.png";
$image1 = imageCreateFromPNG($image1Url);
$image2 = imageCreateFromPNG($image2Url);
$image3 = imageCreateFromPNG($image3Url);

$colorTransparent = imagecolorat($image1, 0, 0);
imageColorTransparent ($image1, $colorTransparent);

$colorTransparent = imagecolorat($image2, 0, 0);
imageColorTransparent ($image2, $colorTransparent);

$colorTransparent = imagecolorat($image3, 0, 0);
imageColorTransparent ($image3, $colorTransparent);

imageCopyMerge($image1, $image2, 0, 0, 0, 0, 96, 96, 100);

imageCopyMerge($image1, $image3, 0, 0, 0, 0, 96, 96, 80);

ImagePng ($image1);


ImageDestroy ($image1);
ImageDestroy ($image2);

推荐答案

所以我在这里结束了

<?php
header('Content-type: image/png');

function imageComposeAlpha( &$src, &$ovr, $ovr_x, $ovr_y, $ovr_w = false, $ovr_h = false, $opc = 127){
 imagecopy($src, $ovr, $ovr_x, $ovr_y, 0, 0, imagesx($ovr), imagesy($ovr) );
}

////////////////////////---////////-----------------------------------

$url = "../../images/socom_font/normal/Socom";

//Covert the String iinto an Array
 $letter = str_split(strtoupper ($_GET['name']));
//Populate Letters Image Path
 foreach($letter as $a){
   //exeptions
  if($a == "?"){ $a = "Question"; }
  if($a == "/"){ $a = "Slash"; }
  if($a == "%"){ $a = "Percent"; }
  if($a == " "){ $a = "Space"; }
  $image[] = $url.$a.".png";
 }unset($a); 
//Create the Letters Image Objects
 foreach($image as $a){
  $image['obj'][] = imageCreateFromPNG($a);
 }unset($a);
//calculate Canvas Width
 foreach($image['obj'] as $a){
  if(!isset($canvasW)){ $canvasW = 0; }
  $canvasW = imagesx($a) + $canvasW;
 }unset($a);
//Create Canvas
 $photoImage = imagecreatetruecolor($canvasW,100);
 imagesavealpha($photoImage, true);
 $trans_color = imagecolorallocatealpha($photoImage, 0, 0, 0, 127);
 imagefill($photoImage, 0, 0, $trans_color);

//Merge Images
 foreach($image['obj'] as $a){
  $width = ceil(imagesx($a));
  if(!isset($offset)){ $offset = 1; }

  imageComposeAlpha($photoImage, $a, $offset, 0,$width,100);

  if($offset >= 1){
   $offset = $offset + $width;
  }
 }unset($a);
// Save it
 //Imagepng($photoImage, 'done.png'); 
// Output to browser 
 Imagepng($photoImage); 

//Destroy all Image Objects
 foreach($image['obj'] as $a){
  ImageDestroy($a);
 }
 ImageDestroy($photoImage);
?>

实时链接 http://thechozenfew.net/projects/font/ImageMerge.php?name = it%20works

这篇关于使用PHP脚本将多个图像合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆