首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PHP中合并图片

在PHP中合并图片
EN

Stack Overflow用户
提问于 2012-02-18 14:18:10
回答 1查看 299关注 0票数 0

我有一个网站www.iniciativa-iex.com,它与twitter相连,它使用API为每个用户获取数据,并在滚动体上显示他/她的个人资料图片。我过去经常在网站被调用时获取数据,但这是一个真正的问题,因为它打了很多电话,我的应用程序令牌被删除得非常频繁。现在我让它加入所有的照片,

代码语言:javascript
复制
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来工作,anny建议?

图像滚筒http://www.iniciativa-iex.com/cron/users.png

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-18 15:46:37

我不会为您编写完整的代码,但是我将为您提供我的函数之一,它创建了如下图像:

代码语言:javascript
复制
<img1> <img2>
<img3> <img4>
<img5> <img6>

从六张照片上。我的脚本假设每个图像的大小都是240x20,您必须按自己的方式完成。

代码语言:javascript
复制
// 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()

代码语言:javascript
复制
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);
}

或者根据你的需要更新它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9341967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档