tags:

views:

335

answers:

4

how i can merge two images in php without GD

+2  A: 

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

robertbasic
+7  A: 

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)

Mr-sk
Good call Emil on http://bg.php.net/imagick forgot about that
Mr-sk
If you can install the imagick extension, why can't you install GD? For that matter, why can't you use GD? It's been enabled by default since forever...
Charles
heh yeah - I'd really push to use GD.
Mr-sk
+3  A: 

You can use Imagick.

Emil Ivanov
A: 

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);
?> 
farshad Ghazanfari
This is using GD
thisMayhem