Mostafa Elkady
Mostafa Elkady

Reputation: 5801

How can I merge two images in PHP without GD?

How can I merge two images in PHP without GD?

Upvotes: 2

Views: 1322

Answers (4)

Farshad
Farshad

Reputation: 1015

i used this block of codes in my project to merge various images into one:

<?php

/* example invocation: http://www.yourserver.com/combine.php?dir=/images/ */

set_time_limit(5*60);

function sanitize($input) { 
    $input=strip_tags($input); 
    $input=str_replace("<","<",$input); 
    $input=str_replace(">",">",$input); 
    $input=str_replace("#","%23",$input); 
    $input=str_replace("'","`",$input); 
    $input=str_replace(";","%3B",$input); 
    $input=str_replace("script","",$input); 
    $input=str_replace("%3c","",$input); 
    $input=str_replace("%3e","",$input); 
    $input=trim($input); 
    return $input; 
} 


//accept path to images via http param dir (e.g. '/templates/corporate/images/okb/' -- include trailing slash)
$rel = '';
if (array_key_exists("dir", $_REQUEST)) $rel = sanitize($_REQUEST["dir"]);
if ($rel=='') die(); 
$rt = $_SERVER['DOCUMENT_ROOT'] . $rel;

$i = 0;
$imgBuf = array ();
$maxW=0; $maxH=0;
$imagesperline = 5;
$curlineW = 0;
$curlineMaxH = 0;
$imagespacing=5;

$dir = opendir ($rt);

while (false !== ($link = readdir($dir))) 
{

    $len = strlen($link);
    $off = $len - 3;
    $ext = substr($link, $off, 3);

    $file = $rt . $link;    

    switch($ext)
    {
        case 'png':
            $iTmp = imagecreatefrompng($file);
            break;
        case 'gif':
            $iTmp = imagecreatefromgif($file);
            break;                
        case 'jpeg':            
        case 'jpg':
            $iTmp = imagecreatefromjpeg($file);
            break;
        default:
            continue;                
    }

    array_push ($imgBuf,$iTmp);

    $i++;
    if ($i == $imagesperline + 1)
    {
       $i = 0;
       $maxW=($maxW>$curlineW)?$maxW:$curlineW;
       $curlineW = 0;
       $maxH+=$curlineMaxH+$imagespacing;
       $curlineMaxH=0;
    }
    else
    {
      $ix = imagesx($iTmp);
      $iy = imagesy($iTmp);
      $curlineW+=$ix+$imagespacing;
      $curlineMaxH=($iy>$curlineMaxH)?$iy:$curlineMaxH;
    }       
}

$iOut = imagecreate ($maxW,$maxH) ;
$i=1;
$curxpos = 0;
$curypos = 0;
$curlineMaxH=0;

foreach ($imgBuf as $img)
{
    if ($i <= $imagesperline)
    {
        $ix = imagesx($img);
        $iy = imagesy($img);

        imagecopy ($iOut,$img,$curxpos,$curypos,0,0,$ix,$iy);
        $curxpos+=$ix + $imagespacing;
        $curlineMaxH=($iy>$curlineMaxH)?$iy:$curlineMaxH;
    }
    else
    {
        $i = 0;
        $curxpos = 0;
        $curypos += $curlineMaxH + $imagespacing;
        $curlineMaxH = 0;
        imagecopy ($iOut,$img,$curxpos,$curypos,0,0,$ix,$iy);    
    }
    imagedestroy ($img);
    $i++;
}

imagegif($iOut);

closedir ($dir);
?> 

Upvotes: -1

Emil Ivanov
Emil Ivanov

Reputation: 37663

You can use Imagick.

Upvotes: 3

mr-sk
mr-sk

Reputation: 13417

PHP doesn't have the built in support to do what your asking. You'll need to

  • Execute another command line script/program that can

  • Install one of the many image manipulation libs and work with that

  • Possibly find a 3rd party API that you can send a request and get a response of the merged images?

  • Do as Emil suggested (Requires install: http://bg.php.net/manual/en/imagick.setup.php)

Upvotes: 7

robertbasic
robertbasic

Reputation: 4335

You can't do image processing in PHP without some library like GD or ImageMagick.

Upvotes: 2

Related Questions