如何在PHP中连接两个图像以创建精灵?

时间:2019-05-08 16:15:27

标签: php image-processing

我的用户可以上传两个图像,我必须同时创建两个图像的精灵。我正在努力寻找如何将它们合并为白色图像的方法。

我想到使用imagecopymerge()并将源图像放置在目标图像的右侧。创建一个白色PNG,然后再次使用imagecopymerge()和我的第一个imagecopymerge()和白色PNG的结果。

function createimage() // Create the white PNG image where the two images concatenated will be stock
{
    public $img = imagecreate(1000, 1000);
    $white = imagecolorallocate($img, 255, 255, 255);
    imagecolortransparent($img, $white);
    imagepng($img, "sprite/image.png");
}


  // Trying to concatenate both images
  $first = imagecreatefrompng("fb.png");
  $second = imagecreatefrompng("izi.png");
  list($width, $height) = getimagesize("izi.png");
  list($widthsource, $heightsource) = getimagesize("fb.png");
  imagecopymerge($second, $first, $width, $height / 2, 0, 0, $widthsource, $heightsource, 100);
  imagepng($second, 'image.png');

  // Then merge my two images concatenate with the white PNG image
  createimage();
  list($widthvierge, $heightvierge) = getimagesize('sprite/image.png');
  imagecopymerge($img, $second, $widthvierge / 2, $heightvierge / 2, 0, 0, $widthvierge, $heightvierge, 100);
  imagepng($img, 'sprite/newsprite.png');

1 个答案:

答案 0 :(得分:0)

您可以使用此类: http://innvo.com/1315192249-css-sprites-with-php/

<?php

class images_to_sprite {

    function images_to_sprite($folder,$output,$x,$y) {
        $this->folder = ($folder ? $folder : 'myfolder'); // Folder name to get images from, i.e. C:\myfolder or /home/user/Desktop/folder
        $this->filetypes = array('jpg'=>true,'png'=>true,'jpeg'=>true,'gif'=>true); // Acceptable file extensions to consider
        $this->output = ($output ? $output : 'mysprite'); // Output filenames, mysprite.png and mysprite.css
        $this->x = $x; // Width of images to consider
        $this->y = $y; // Heigh of images to consider
        $this->files = array();
    }

    function create_sprite() {

        $basedir = $this->folder;
        $files = array();

        // Read through the directory for suitable images
        if($handle = opendir($this->folder)) {
            while (false !== ($file = readdir($handle))) {
                $split = explode('.',$file);
                // Ignore non-matching file extensions
                if($file[0] == '.' || !isset($this->filetypes[$split[count($split)-1]]))
                    continue;
                // Get image size and ensure it has the correct dimensions
                $output = getimagesize($this->folder.'/'.$file);
                if($output[0] != $this->x && $output[1] != $this->y)
                    continue;
                // Image will be added to sprite, add to array
                $this->files[$file] = $file;
            }
            closedir($handle);
        }

        // yy is the height of the sprite to be created, basically X * number of images
        $this->yy = $this->y * count($this->files);
        $im = imagecreatetruecolor($this->x,$this->yy);

        // Add alpha channel to image (transparency)
        imagesavealpha($im, true);
        $alpha = imagecolorallocatealpha($im, 0, 0, 0, 127);
        imagefill($im,0,0,$alpha);

        // Append images to sprite and generate CSS lines
        $i = $ii = 0;
        $fp = fopen($this->output.'.css','w');
        fwrite($fp,'.'.$this->output.' { width: '.$this->x.'px; height: '.$this->y.'px; background-image: url('.$this->output.'.png); text-align:center; }'."\n");
            foreach($this->files as $key => $file) {
            fwrite($fp,'.'.$this->output.(++$ii).' { background-position: -0px -'.($this->y*$i).'px; }'."n");
            $im2 = imagecreatefrompng($this->folder.'/'.$file);
            imagecopy($im,$im2,0,($this->y*$i),0,0,$this->x,$this->y);
            $i++;
            }
        fclose($fp);
        imagepng($im,$this->output.'.png'); // Save image to file
        imagedestroy($im);
    }
}

$class = new images_to_sprite('imagefolder','sprite',63,63);
$class->create_sprite();

或交替使用此:

<?php

/* Two images */
$image1 = dirname(__FILE__).'/image1.png';
$image2 = dirname(__FILE__).'/image2.png';

/* Get images dimensions */
$size1 = getimagesize($image1);
$size2 = getimagesize($image2);

/* Load the two existing images */
$im1 = imagecreatefrompng($image1);
$im2 = imagecreatefrompng($image2);

/* Create the new image, width is combined but height is the max height of either image */
$im = imagecreatetruecolor($size1[0] + $size2[0], max($size1[1], $size2[1]));

/* Merge the two images into the new one */
imagecopy($im, $im1, 0, 0, 0, 0, $size1[0], $size1[1]);
imagecopy($im, $im2, $size1[0], 0, 0, 0, $size2[0], $size2[1]);

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

imagepng($im);
imagedestroy($im);

我希望这会有所帮助!