用PHP合并图片

时间:2012-02-18 14:18:10

标签: php image cron

我有一个网站www.iniciativa-iex.com,它与Twitter相关联,它为每个用户使用API​​获取数据,并在滚动条上显示他/她的个人资料图片。我曾经在每次调用网站时获取数据,但这是一个真正的问题,因为它进行了大量调用,我的应用令牌被频繁删除。现在我把它加入了所有图片,

include '../twitter/LibTwitter.php';
$sql = mysql_query("SELECT * FROM `users` WHERE `suspended` != 'true' ORDER BY id DESC");
$fullW = mysql_num_rows($sql)*128;
$fullH = 128;$width1 = 0;
$img = imagecreate($fullW, $fullH);
while($result = mysql_fetch_array($sql)) {
    $userid = $result["userid"];
    $busqueda = $twitter->usersShow($userid, null);
    $src = $busqueda["profile_image_url"];
    $filetype = str_replace(".", "", substr($src, -4));
    if ($filetype == "rmal"){$filetype = "";} 
    $file = str_replace("_normal.".$filetype, "_reasonably_small.".$filetype, $src);
    switch($filetype){
            case 'png':
                $sub = imagecreatefrompng($file) or die( "Cannot open $filetype file `$file - $src` where USER = `$userid`\n");
            break;
            case 'gif':
                $sub = imagecreatefromgif($file) or die( "Cannot open $filetype file `$file - $src` where USER = `$userid`\n");
            break;               
            case 'jpeg':           
            case 'jpg':
            case 'JPG':
            case '':
                $sub = imagecreatefromjpeg($file) or die( "Cannot open $filetype file `$file - $src` where USER = `$userid`\n");
            break;            
    }
        if(!$sub){die('Missing sub');}imagecopy ( $img, $sub, $width1 , 0, 0, 0, 128, 128);
        // imagecopy ( $img, $sub, $width1 , 0, 0, 0, $width, $height) or die( "Cannot copy $filetype file `$file - $src` where USER = `$userid`\n");
        $width1 = $width1 + 128;
        imagedestroy($sub);
}
imagepng($img, 'users.png');
imagedestroy($img);

编辑:我的代码正常运行,但现在它显示的是扭曲的,质量不佳的图片,并且需要CHMOD 0777才能正常工作,这是一个暗示吗?

IMAGE SCROLLER http://www.iniciativa-iex.com/cron/users.png

谢谢!

1 个答案:

答案 0 :(得分:1)

我不打算为你编写完整的代码,但是我会为你提供我的一个功能,它创建了这样的图像:

<img1> <img2>
<img3> <img4>
<img5> <img6>

来自六张照片。我的脚本假设每张图片的尺寸为240x20,您必须按照自己的方式进行操作。

// Sizes of one image
$width = 240;
$height = 20;

// The whole image
$fullW = $width*2;
$fullH = $height*3;

// Allocate image with exact size for 6 images
$img = imagecreate( $fullW, $fullH) or die("Cannot Initialize new GD image stream\n");;

// I was creating images based on their name, so here're name parts
$parts = array( 'real', 'imaginary');
$keys = array( 'normalized', 'code', 'code2');

// Two loops iterating trough all images, you'll be doing it one probably
foreach( $parts as $i => $part){
    foreach( $keys as $j => $key){
        // Generate name
        $file = $prefix . '_' . $part . '_' . $key . '.png';

        // I didn't need any special handling for errors, add your own, destroy image
        // if anything goes wrong and so on
        $sub = imagecreatefrompng( $file) or die( "Cannot open png file `$file`\n");

        // After looking this function up in manual everything should be clear
        imagecopy ( $img, $sub, $i*$width, $j*$height, 0, 0, $width, $height) or die( "Cannot copy data from `$file`\n");

        // Unload current image
        imagedestroy( $sub);
    }
}

// Save image
imagepng( $img, $prefix . '.png');
echo "Saving: $prefix.png\n";
imagedestroy( $img);

编辑:图片不够大

如果您的图片($sub)不够大(例如。40x40 px),您应检查其尺寸并使用imagecopyresampled()

if( (imagesx( $sub) < $width) || (imagesy( $sub) < $height)){
    imagecopyresampled( $img, $sub, $width1, 0, 0, 0, $width, $height, imagesx( $sub), imagesy( $sub));
} else {
    imagecopy ( $img, $sub, $width1 , 0, 0, 0, $width, $height);
}

或者根据需要更新它。