在PHP中合并图片
我有一个网站在PHP中合并图片,php,image,cron,Php,Image,Cron,我有一个网站www.iniciativa-iex.com,它与twitter相连,它使用API为每个用户获取数据,并在滚动条上显示他/她的个人资料图片。我以前每次访问网站时都会获取数据,但这是一个真正的问题,因为它打了很多电话,而且我的应用令牌被频繁删除。现在我让它加入所有的图片 include '../twitter/LibTwitter.php'; $sql = mysql_query("SELECT * FROM `users` WHERE `suspended` != 'tru
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才能正常工作,安妮建议
谢谢 我不打算为您编写全部代码,但我将为您提供一个创建如下图像的函数:
<img1> <img2>
<img3> <img4>
<img5> <img6>
编辑:图像不够大
如果您的图像($sub
)不够大(例如40x40
px),您应该检查它们的大小并使用:
if((imagesx($sub)<$width)| |(imagesy($sub)<$height)){
imagecopyresampled($img,$sub,$width1,0,0,$width,$height,imagesx($sub),imagesy($sub));
}否则{
imagecopy($img,$sub,$width1,0,0,$width,$height);
}
或者根据您的需要进行更新。我完成了这项工作,但现在它不再工作了,有什么问题吗?黑色图像:(@Luis pastebin.com(或类似的东西)你的代码,这就像问我为什么头痛……)这不是问题,每个图片是128x128px,不知道是哪个问题!如果可能,还需要CHMOD问题的帮助;)谢谢我认为这将帮助你不真正准确的我想要什么,但它将有助于在另一个功能,我写的。谢谢;)是的,我使用了die(),它不会打印错误!编辑此:
imagecopy($img,$sub,$width1,0,0,$width,$height)代码>=>if(!$sub){die('Missing sub');}imagecopy($img,$sub,$width1,0,0,0,$width,$height)代码>废话…:)<代码>图像复制($img,$sub,$width1,0,0,0,$width,$height)代码>宽度($width
和高度($height
?)的值是多少
// 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);
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);
}