【问题标题】:How do I concatenate two images in PHP to create a sprite?如何在 PHP 中连接两个图像以创建精灵?
【发布时间】:2019-09-26 10:30:47
【问题描述】:

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

我的想法是使用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');

【问题讨论】:

    标签: php image-processing


    【解决方案1】:

    你可以使用这个类: 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);
    

    我希望这会有所帮助!

    【讨论】:

    • 有没有办法不用上课?否则谢谢你的帮助:)
    • @AyoubShakur 当然,我刚刚编辑了我的答案以添加我刚刚尝试过的替代解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2013-03-07
    相关资源
    最近更新 更多