在PHP中合并图片 [英] Merge pictures in PHP
问题描述
我有一个网站 www.iniciativa-iex.com
,它与twitter连接,它使用API为每个用户的数据,并显示他/她的个人资料图片在卷轴上。我曾经每次访问网站时获取数据,但这是一个真正的问题,因为它做了很多电话,我的应用程序令牌被频繁删除。现在我把它加入所有的图片,
I have a website www.iniciativa-iex.com
, it's connected with twitter, it get's the data using the API for each user and displays his/her profile picture on the scroller. I used to get the data each time the website was called but it was a real problem because it made a lot of calls and my app tokens were deleted very frequently. Now I made it join all the pictures,
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建议?
I got the code working but now it shows a distorted, bad-quality picture, and need CHMOD 0777 to work, anny suggestion?
谢谢!
推荐答案
我不会为你写整个代码,但我会为你提供一个我的函数创建的图像如下:
I'm not going to write your whole code for you, but I'll provide you with one of my function which created image like this:
<img1> <img2>
<img3> <img4>
<img5> <img6>
从六张图片。我的脚本假设每个图像的大小 240x20
,你必须以你的方式。
From six images. My script assumes that size of each image is 240x20
, you'll have to do it your way.
// 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()
:
images not big enough
If your images ($sub
) aren't large enough (eg. 40x40
px) you should check their size and use 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);
}
或根据需要更新。
这篇关于在PHP中合并图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!