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 は存在しないため、$image1 の $im を変更することをお勧めします。

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 に答える