用GD创建一张包含其他图像的图片

发布于 2024-09-03 06:02:49 字数 384 浏览 3 评论 0原文

我想用PHP用GD创建一张由不同的其他图片组成的图片。例如,我有 6 张图片(或更多),我想创建一张包含这些不同图片的图片。

困难在于我的最终图片必须具有固定的宽度和高度(304x179),因此如果不同的图片太大,则必须将其剪切。这是来自 IconFinder 的示例:

这张图片有 6 张图片

这张图片由 6 张图片组成,但第 3 只鸟(绿色)被剪掉,第 4、5、6 只被剪在底部。这就是我想要的,你能帮我用 PHP 编写这段代码吗?

谢谢

I would like to create a picture in PHP with GD composed by different other pictures. For example I have 6 pictures (or more) and I would like to create ONE picture who contain these different pictures.

The Difficulty is that my final picture must have a fixed width and height (304x179), so if the different pictures are too big they must be cut. This is an example from IconFinder :

This picture have 6 images

This picture is composed by 6 images, but the 3rd bird (green) is cutted, and the 4, 5 and 6 are cutted in the bottom. This is what I want, can you give me some help to write this code in PHP ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雨成花 2024-09-10 06:02:49

创建您的主要图像并将其视为您的“画布”。

从那里,使用 imagecopy() 将较小的图像复制到画布中图像。

例如:

<?php
header('Content-Type: image/jpg');
$canvas = imagecreatetruecolor(304, 179);
$icon1 = imagecreatefromjpeg('icon.jpg');
$icon2 = imagecreatefromjpeg('icon2.jpg');
// ... add more source images as needed
imagecopy($canvas, $icon1, 275, 102, 0, 0, 100, 100);
imagecopy($canvas, $icon2, 0, 120, 0, 0, 100, 100);
// ... copy additional source images to the canvas as needed
imagejpeg($canvas);
?>

在我的示例中,icon.jpg 是一个 100x100 的图像,我将其放置在画布中,使其左上角位于画布中的 275, 102 处,这会截断右侧。

编辑

我调整了代码,使其更类似于您正在做的事情。

Create your primary image and consider it your "canvas."

From there, use imagecopy() to copy the smaller images into the canvas image.

See this for example:

<?php
header('Content-Type: image/jpg');
$canvas = imagecreatetruecolor(304, 179);
$icon1 = imagecreatefromjpeg('icon.jpg');
$icon2 = imagecreatefromjpeg('icon2.jpg');
// ... add more source images as needed
imagecopy($canvas, $icon1, 275, 102, 0, 0, 100, 100);
imagecopy($canvas, $icon2, 0, 120, 0, 0, 100, 100);
// ... copy additional source images to the canvas as needed
imagejpeg($canvas);
?>

In my example, icon.jpg is a 100x100 image which I am placing in the canvas such that its top left corner is located at 275, 102 in the canvas, which cuts off the right side.

Edit

I adjusted the code to be more similar to what you're doing.

对你的占有欲 2024-09-10 06:02:49

这是我的一个脚本中未经测试的修改 spinet,希望它有用:

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

    $image = array() //Populate this array with the image paths

    //Create the Letters Image Objects
      foreach($image as $a){
        $image['obj'][] = imageCreateFromPNG($a);
      }unset($a);

      $canvasW = 300;
      $canvasH = 300;

    //Create Canvas
      $photoImage = imagecreatetruecolor($canvasW,$canvasH);
      imagesavealpha($photoImage, true);
      $trans_color = imagecolorallocatealpha($photoImage, 0, 0, 0, 127);
      imagefill($photoImage, 0, 0, $trans_color);

    //Merge Images
      $Offset_y = 0;
      $images_by_row = 3;
      $images_rows_height = 100; // height of each image row
      $counter = 0;

      foreach($image['obj'] as $a){
        $counter++;

        $width = ceil(imagesx($a));
        $height = ceil(imagesy($a));

        if(!isset($offset)){ $offset = 1; }

        imageComposeAlpha($photoImage, $a, $offset, $Offset_y,$width,$height);

        if($offset >= 1){
          $offset = $offset + $width;
        }

        //Check if new row next time
        if($counter >= $images_by_row){
          if($images_by_row%$counter){
            $offset_y += $images_rows_height;
          }
        }

      }unset($a);

      imagepng($photoImage);

Here a none tested modify spinet from one of my scripts, hope it can be usefull:

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

    $image = array() //Populate this array with the image paths

    //Create the Letters Image Objects
      foreach($image as $a){
        $image['obj'][] = imageCreateFromPNG($a);
      }unset($a);

      $canvasW = 300;
      $canvasH = 300;

    //Create Canvas
      $photoImage = imagecreatetruecolor($canvasW,$canvasH);
      imagesavealpha($photoImage, true);
      $trans_color = imagecolorallocatealpha($photoImage, 0, 0, 0, 127);
      imagefill($photoImage, 0, 0, $trans_color);

    //Merge Images
      $Offset_y = 0;
      $images_by_row = 3;
      $images_rows_height = 100; // height of each image row
      $counter = 0;

      foreach($image['obj'] as $a){
        $counter++;

        $width = ceil(imagesx($a));
        $height = ceil(imagesy($a));

        if(!isset($offset)){ $offset = 1; }

        imageComposeAlpha($photoImage, $a, $offset, $Offset_y,$width,$height);

        if($offset >= 1){
          $offset = $offset + $width;
        }

        //Check if new row next time
        if($counter >= $images_by_row){
          if($images_by_row%$counter){
            $offset_y += $images_rows_height;
          }
        }

      }unset($a);

      imagepng($photoImage);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文