繁体   English   中英

如何在PHP中将多个图像拼接成一个单独的图像?

[英]How to stitch multiple images into one single image in php?

我正在建立一个Facebook应用程序,我需要将多个配置文件图片切换成一个大图像。 有没有办法在PHP?

我是这个问题的新手,但是我正在开发一个脚本来拍摄图像并将它们缝合在一起。 现在它只有在所有图像大小相同的情况下才有效,但我正在努力改变它,我欢迎贡献。 我用它来制作网站的CSS图像悬浮/非悬浮状态图形:

    /**
     * Image stitching function.
     *
     * Now operates with images of varying heights as well as widths.
     *
     * @param array $files
     * An array of files. Each element is a path to the file in question.
     *
     * @param int $rows
     * The number of rows the end resulting image will have. The images
     * will be added to the new image in the order of the array divided
     * equally in number to the rows specified here.
     * 
     * @param int $action
     * An integer (or static define) of the action to take on the resulting
     * image.
     *  IMAGE_STITCH_DISPLAY - Display the item (default action).
     *  IMAGE_STITCH_SAVE - Save the image to the file system (path required).
     *  IMAGE_STITCH_RETURN - Return the resulting file pointer to the calling
     *                       function for processing there.
     *
     * @param string $path
     * The path of where to save the resulting new image.
     *
     * @return image $image
     * The image data that can have whatever done to it.
     */
    function image_stitch($files, $rows = 2, $action = IMAGE_STITCH_DISPLAY, $path = NULL) {
      foreach($files as $file) {
        $path = explode('.', $file);
        if ($path[count($path)-1] == 'png') {
          $images[] = imagecreatefrompng($file);
        } 
        else {
          $images[] = imagecreatefromjpeg($file);
        }
      }
      $number_of_images = count($images);
      $number_of_columns = ($number_of_images / $rows) - 1;
      $total_width = 0;
      $max_width = 0;
      $total_heights = array();
      $widths = array(array());
      $grid = array(array());
      for ($y = 1; $y <= $rows; $y++) {
        $this_height = $this_width = 0; 
        for ($x = 0; $x <= $number_of_columns; $x++) {
          if (empty($files[(( $y + ($x - 1)) + ($number_of_columns * ($y - 1)))])) {
            next;
          }
          $image_size = getimagesize($files[(( $y + ($x - 1)) + ($number_of_columns * ($y - 1)))]);
          $grid[$x][$y] = $images[(( $y + ($x - 1)) + ($number_of_columns * ($y - 1)))];
          $width = $image_size[0];
          $height = $image_size[1];
          $widths[$x][$y][] = $width;

          $this_width += $width;
          if ($height > $this_height) {
            $this_height = $height;
          }

          if ($x == 0 && $y > 1) {
            $total_heights[] = $this_height;
            if ($max_width < $this_width) {
              $max_width = $this_width;
            }
          }
        }
      }

      $total_heights[] = $this_height;
      if ($max_width < $this_width) {
        $max_width = $this_width;
      }

      $destination_image = imagecreatetruecolor($max_width, array_sum($total_heights));
      $black = imagecolorallocate($destination_image, 0, 0, 0);
      imagecolortransparent($destination_image, $black);
      imagealphablending($destination_image, FALSE);
      imagesavealpha($destination_image, TRUE);

      // place our images
      foreach($grid as $instance_key => $instance) {
        $height = $total_heights[$instance_key];
        foreach($instance as $reference_key => $reference) {
          imagecopyresampled($destination_image, $reference, $instance_key * 180, ($reference_key - 1) * 180, 0, 0, 180, 180, 180, 180);
        }
      }

      // Display the image if directed
      if ($action = IMAGE_STITCH_DISPLAY) {
        header('content-type: image/png');
        imagepng($destination_image);
        imagedestroy($destination_image);
        exit();
      }

      // Return the image if directed.
      if ($action == IMAGE_STITCH_RETURN) {
        return $destination_image;
      }

      // If we are saving the image, save it with no compression or filters.
      if (!$empty($path)) {
        imagepng($destination_image, $path, 0, PNG_NO_FILTER);
      }

      return TRUE;
    }

请参见http://www.php.net/manual/en/function.imagecopy.php上的示例,并多次使用imagecopy()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM