0

我设法用这段代码在 PHP 中合并了 3 个图像:

header ("Content-type: image/jpeg"); 

$image1Url = "1.jpg"; 
$image2Url = "2t.jpg"; 
$image3Url = "3.jpg"; 
$image1 = imageCreateFromjpeg($image1Url); 
$image2 = imageCreateFromjpeg($image2Url); 
$image3 = imageCreateFromjpeg($image3Url); 

$colorTransparent = imagecolorat($image1, 0, 0); 
$colorTransparent = imagecolorat($image2, 0, 0); 
$colorTransparent = imagecolorat($image3, 0, 0); 


imagecopymerge($image1, $image2, 20, 9, 0, 0, 240, 240, 100); 
imageCopyMerge($image1, $image3, 200, 10, 0, 0, 60, 40, 100); 

Imagejpeg ($image1); 


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

我现在想添加一些文本,所以我的最终代码是:

header ("Content-type: image/jpeg"); 

$image1Url = "1.jpg"; 
$image2Url = "2.jpg"; 
$image3Url = "3.jpg"; 
$image1 = imageCreateFromjpeg($image1Url); 
$image2 = imageCreateFromjpeg($image2Url); 
$image3 = imageCreateFromjpeg($image3Url); 

$colorTransparent = imagecolorat($image1, 0, 0); 
$colorTransparent = imagecolorat($image2, 0, 0); 
$colorTransparent = imagecolorat($image3, 0, 0); 


imagecopymerge($image1, $image2, 20, 9, 0, 0, 240, 240, 100);
imageCopyMerge($image1, $image3, 200, 10, 0, 0, 60, 40, 100); 
$text = "Username";
$font = "Font.ttf";
$black = imagecolorallocate($im, 0, 0, 0);

imagettftext($image1, 10, 0, 217, 15, $black, $font, $text);
Imagejpeg ($image1); 


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

但是图片没有显示

4

2 回答 2

0

我认为问题出在这一行:

$black = imagecolorallocate($im, 0, 0, 0);

Var $im 不存在,所以我建议您将 $im 更改为 $image1。

我假设 Font.ttf 是可以访问的。

于 2014-05-21T12:33:53.387 回答
0

正如我所做的那样,它解决了 3 个主要问题(检查单独的文件路径并从 db 收集 txt)并且我认为它更容易调试。

    if (file_exists($img_base_path)) {
    if (file_exists($img_photo_path)) {
      if (file_exists($img_sign_path) && mime_content_type($img_sign_path)=="image/jpeg") {

      $img_base=imagecreatefromjpeg($img_base_path);
      $img_photo =imagecreatefromjpeg($img_photo_path);
      $img_sign =imagecreatefromjpeg($img_sign_path);
      ob_clean();
      $s_name=strtoupper($row['s_name']); $reg_no=$row['register']; $address=$row['address'].', '.$row['pincode'].', '.$row['state']; $s_contact=$row['s_contact'];$dob=$row['dob_year'].'.'.$row['dob_month'].'.'.$row['dob_day'].' (Y.M.D)';$c_code=$row['c_code'];$study_center=$row['c_code'];$fname=strtoupper($row['f_name']);
      header('content-type:image/jpeg');

      $font="../_static/arial.ttf";

      imagecolorat($img_base, 0, 0); 
      imagecolorat($img_photo, 0, 0); 
      imagecolorat($img_sign, 0, 0);

      imagecopymerge($img_base, $img_photo, 1830, 650, -2, 10, 200, 240, 100);  //  $color=imagecolorallocate($img_base, 19,21,22);
      imagecopymerge($img_base, $img_sign, 1830, 850, -2, 10, 200, 240, 100);     

            $color=imagecolorallocate($img_base, 19,21,22);


      imagettftext($img_base, 30, 0, 760,925, $color, $font,$s_name);
      imagettftext($img_base, 45, 0, 900,810 , $color, $font,$reg_no);
      imagettftext($img_base, 30, 0, 760,995 , $color, $font,$fname);
      imagettftext($img_base, 30, 0, 760,1070 , $color, $font,$dob);
      imagettftext($img_base, 30, 0, 760,1130 , $color, $font,$c_code);


      imagejpeg($img_base);
      imagedestroy($img_base);
      imagedestroy($img_photo);
      imagedestroy($img_sign);

      }   else echo $img_sign_path,mime_content_type($img_sign_path);
    }   else echo $img_photo_path;

}   else echo $img_base_path;
于 2020-12-26T07:00:27.157 回答