Php 创建动态PNG图像
我想用PHP创建一个小函数,它接受颜色、形状、透明度等参数,并输出PNG图像。我听说过PHP GD库,但我想知道如何创建像您使用PHP GD库几乎可以做任何事情一样具有创造性的东西。虽然有可能,但创建一个像你描述的那样的图像是相当困难的,因为我做了一些奇怪的事情,包括渐变、循环和颜色 如果你想根据某些参数动态生成这样的图像,你可以事先在photoshop中创建图像,然后根据用户的选择覆盖它们 你可以享受很多乐趣 编辑:哦,顺便说一下,如果你感兴趣的话,是谁负责创建图像并导致错误。这将是一个了解代码的好地方 第二次编辑:这只是我用这种技术做的事情。记住那是很久以前的事了。它接受基于查询字符串的名称,并基本上使用大量随机数执行几个循环 这是源代码,我为任何愚蠢的代码/引用道歉。这是很久以前写的,当时我大约14岁,我相信(可能有很多缺陷)Php 创建动态PNG图像,php,image,png,Php,Image,Png,我想用PHP创建一个小函数,它接受颜色、形状、透明度等参数,并输出PNG图像。我听说过PHP GD库,但我想知道如何创建像您使用PHP GD库几乎可以做任何事情一样具有创造性的东西。虽然有可能,但创建一个像你描述的那样的图像是相当困难的,因为我做了一些奇怪的事情,包括渐变、循环和颜色 如果你想根据某些参数动态生成这样的图像,你可以事先在photoshop中创建图像,然后根据用户的选择覆盖它们 你可以享受很多乐趣 编辑:哦,顺便说一下,如果你感兴趣的话,是谁负责创建图像并导致错误。这将是一个了解代
以下是我以前用来生成具有两个名称的图像的代码,这两个名称可以从查询字符串参数中接受。我使用准备好的背景图片,并把名字放在上面
<?php
// Print two names on the picture, which accepted by query string parameters.
$n1 = $_GET['n1'];
$n2 = $_GET['n2'];
Header ("Content-type: image/jpeg");
$image = imageCreateFromJPEG("images/someimage.jpg");
$color = ImageColorAllocate($image, 255, 255, 255);
// Calculate horizontal alignment for the names.
$BoundingBox1 = imagettfbbox(13, 0, 'ITCKRIST.TTF', $n1);
$boyX = ceil((125 - $BoundingBox1[2]) / 2); // lower left X coordinate for text
$BoundingBox2 = imagettfbbox(13, 0, 'ITCKRIST.TTF', $n2);
$girlX = ceil((107 - $BoundingBox2[2]) / 2); // lower left X coordinate for text
// Write names.
imagettftext($image, 13, 0, $boyX+25, 92, $color, 'ITCKRIST.TTF', $n1);
imagettftext($image, 13, 0, $girlX+310, 92, $color, 'ITCKRIST.TTF', $n2);
// Return output.
ImageJPEG($image, NULL, 93);
ImageDestroy($image);
?>
要在页面上显示生成的图像,请执行以下操作:
<img src="myDynamicImage.php?n1=bebe&n2=jake" />
这不是“用PHP实现”的直接答案,但您可以从PHP调用一些功能强大的命令行软件。特别是将绘制所有内容,包括厨房水槽。它还有一个优点,即可用于“带外”处理的“后端”脚本(即,在请求完成后执行图像处理(更快的用户反馈)或者在高峰时段资源紧张的深夜分批进行。这并不是您想要的,但我编写了一个脚本,将动态颜色层插入到透明图像中。您可以使用单色图像“层”进行设置要运行它,给它一个十六进制颜色代码。脚本会重新给你的图层上色,并将它们合并到一个图像中来呈现。下面是代码;希望你能从中得到一些用处
function hexLighter($hex, $factor = 30) {
$new_hex = '';
$base['R'] = hexdec($hex{0}.$hex{1});
$base['G'] = hexdec($hex{2}.$hex{3});
$base['B'] = hexdec($hex{4}.$hex{5});
foreach ($base as $k => $v) {
$amount = 255 - $v;
$amount = $amount / 100;
$amount = round($amount * $factor);
$new_decimal = $v + $amount;
$new_hex_component = dechex($new_decimal);
$new_hex .= sprintf('%02.2s', $new_hex_component);
}
return $new_hex;
}
// Sanitize/Validate provided color variable
if (!isset($_GET['color']) || strlen($_GET['color']) != 6) {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request', true, 400);
exit(0);
}
if (file_exists( "cache/{$_GET['color']}.png" )) {
header( 'Content-Type: image/png' );
readfile( "cache/{$_GET['color']}.png" );
exit(0);
}
// Desired final size of image
$n_width = 50;
$n_height = 50;
// Actual size of source images
$width = 125;
$height = 125;
$image = imagecreatetruecolor($width, $height);
imagesavealpha($image, true);
imagealphablending($image, false);
$n_image = imagecreatetruecolor($n_width, $n_height);
imagesavealpha($n_image, true);
imagealphablending($n_image, false);
$black = imagecolorallocate($image, 0, 0, 0);
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefilledrectangle($image, 0, 0, $width, $height, $transparent);
$layers = array();
$layers_processed = array();
$layers[] = array( 'src' => 'layer01.gif', 'level' => 0 ); // Border
$layers[] = array( 'src' => 'layer02.gif', 'level' => 35 ); // Background
$layers[] = array( 'src' => 'layer03.gif', 'level' => 100 ); // White Quotes
foreach ($layers as $idx => $layer) {
$img = imagecreatefromgif( $layer['src'] );
$processed = imagecreatetruecolor($width, $height);
imagesavealpha($processed, true);
imagealphablending($processed, false);
imagefilledrectangle($processed, 0, 0, $width, $height, $transparent);
$color = hexLighter( $_GET['color'], $layer['level'] );
$color = imagecolorallocate($image,
hexdec( $color{0} . $color{1} ),
hexdec( $color{2} . $color{3} ),
hexdec( $color{4} . $color{5} )
);
for ($x = 0; $x < $width; $x++)
for ($y = 0; $y < $height; $y++)
if ($black === imagecolorat($img, $x, $y))
imagesetpixel($processed, $x, $y, $color);
imagecolortransparent($processed, $transparent);
imagealphablending($processed, true);
array_push($layers_processed, $processed);
imagedestroy( $img );
}
foreach ($layers_processed as $processed) {
imagecopymerge($image, $processed, 0, 0, 0, 0, $width, $height, 100);
imagedestroy( $processed );
}
imagealphablending($image, true);
imagecopyresampled($n_image, $image, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
imagealphablending($n_image, true);
header( 'Content-Type: image/png' );
imagepng( $n_image, "cache/{$_GET['color']}.png" );
imagepng( $n_image );
// Free up memory
imagedestroy( $n_image );
imagedestroy( $image );
函数hextlighter($hex,$factor=30){
$new_hex='';
$base['R']=hexdec($hex{0}.$hex{1});
$base['G']=hexdec($hex{2}.$hex{3});
$base['B']=hexdec($hex{4}.$hex{5});
foreach($k=>v){
$amount=255-$v;
$amount=$amount/100;
$amount=四舍五入($amount*$factor);
$new_decimal=$v+$amount;
$new_hex_component=dechex($new_decimal);
$new_hex.=sprintf('%02.2s',$new_hex_component);
}
返回$new_hex;
}
//清理/验证提供的颜色变量
如果(!isset($_GET['color'])| | strlen($_GET['color'])!=6){
标头($\u服务器['SERVER\u协议'.'400错误请求',true,400);
出口(0);
}
如果(文件_存在(“cache/{$_GET['color']}.png”)){
标题('Content Type:image/png');
readfile(“cache/{$\u GET['color']}.png”);
出口(0);
}
//所需的图像最终大小
$n_宽度=50;
$n_高度=50;
//源图像的实际大小
$width=125;
$height=125;
$image=imageCreateTureColor($width,$height);
imagesavealpha($image,true);
imagealphablending($image,false);
$n_image=ImageCreateTureColor($n_宽度,$n_高度);
imagesavealpha($n_image,true);
imagealphablending($n_图像,错误);
$black=imagecolorallocate($image,0,0,0);
$transparent=imagecolorallocatealpha($image,255,255,127);
imagefilledrectangle($image,0,0,$width,$height,$transparent);
$layers=array();
$layers_processed=array();
$layers[]=数组('src'=>'layer01.gif','level'=>0);//边框
$layers[]=数组('src'=>'layer02.gif','level'=>35);//背景
$layers[]=数组('src'=>'layer03.gif','level'=>100);//白引号
foreach($idx=>$layer的层){
$img=imagecreatefromformgif($layer['src']);
$processed=ImageCreateTureColor($width,$height);
imagesavealpha($processed,true);
ImageAlphabling($已处理,错误);
imagefilledrectangle($processed,0,0,$width,$height,$transparent);
$color=hextlighter($_GET['color'],$layer['level']);
$color=imagecolorallocate($image,
hexdec($color{0}.$color{1}),
hexdec($color{2}.$color{3}),
hexdec($color{4}.$color{5})
);
对于($x=0;$x<$width;$x++)
对于($y=0;$y<$height;$y++)
如果($black==imagecolorat($img,$x,$y))
imagesetpixel($processed,$x,$y,$color);
imagecolortransparent($processed,$transparent);
imagealphablending($processed,true);
数组\u推送($layers\u processed,$processed);
图像处理(img);
}
foreach($layers\u处理为$processed){
imagecopymerge($image,$processed,0,0,0,$width,$height,100);
图像销毁(已处理);
}
imagealphablending($image,true);
imagecopyresampled($n_图像,$image,0,0,0,$n_宽度,$n_高度,$width,$height);
imagealphablending($n_image,true);
标题('Content Type:image/png');
imagepng($n_image,“cache/{$_GET['color']}.png”);
imagepng($n_image);
//释放内存
imagedestroy($n_image);
图像销毁($图像);
如果您想了解有关该代码的更多信息,我将对我的进行详细解释。以下是一个简单的示例:
<?php
$your_text = "Helloooo Worldddd";
$IMG = imagecreate( 250, 80 );
$background = imagecolorallocate($IMG, 0,0,255);
$text_color = imagecolorallocate($IMG, 255,255,0);
$line_color = imagecolorallocate($IMG, 128,255,0);
imagestring( $IMG, 10, 1, 25, $your_text, $text_color );
imagesetthickness ( $IMG, 5 );
imageline( $IMG, 30, 45, 165, 45, $line_color );
header( "Content-type: image/png" );
imagepng($IMG);
imagecolordeallocate($IMG, $line_color );
imagecolordeallocate($IMG, $text_color );
imagecolordeallocate($IMG, $background );
imagedestroy($IMG);
exit;
?>
这是我用动态文本创建动态png图像的功能。。。 可称为-
<img src="create_image.php?s=008080_F_1000_200&t=Sample%20Image%20Drawn%20By%20PHP" alt="GD Library Example Image" >
下面是create_image.php,它提供了请求的图像
<?php
$setting = isset($_GET['s']) ? $_GET['s'] : "FFF_111_100_100";
$setting = explode("_",$setting );
$img = array();
switch ($n = count($setting)) {
case $n > 4 :
case 3:
$setting[3] = $setting[2];
case 4:
$img['width'] = (int) $setting[2];
$img['height'] = (int) $setting[3];
case 2:
$img['color'] = $setting[1];
$img['background'] = $setting[0];
break;
default:
list($img['background'],$img['color'],$img['width'],$img['height']) = array('F','0',100,100);
break;
}
$background = explode(",",hex2rgb($img['background']));
$color = explode(",",hex2rgb($img['color']));
$width = empty($img['width']) ? 100 : $img['width'];
$height = empty($img['height']) ? 100 : $img['height'];
$string = (string) isset($_GET['t']) ? $_GET['t'] : $width ."x". $height;
header("Content-Type: image/png");
$image = @imagecreate($width, $height)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, $background[0], $background[1], $background[2]);
$text_color = imagecolorallocate($image, $color[0], $color[1], $color[2]);
imagestring($image, 5, 5, 5, $string, $text_color);
imagepng($image);
imagedestroy($image);
function hex2rgb($hex) {
// Copied
$hex = str_replace("#", "", $hex);
switch (strlen($hex)) {
case 1:
$hex = $hex.$hex;
case 2:
$r = hexdec($hex);
$g = hexdec($hex);
$b = hexdec($hex);
break;
case 3:
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
break;
default:
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
break;
}
$rgb = array($r, $g, $b);
return implode(",", $rgb);
}
以下是一个简单的示例:
<?php
$your_text = "Helloooo Worldddd";
$IMG = imagecreate( 250, 80 );
$background = imagecolorallocate($IMG, 0,0,255);
$text_color = imagecolorallocate($IMG, 255,255,0);
$line_color = imagecolorallocate($IMG, 128,255,0);
imagestring( $IMG, 10, 1, 25, $your_text, $text_color );
imagesetthickness ( $IMG, 5 );
imageline( $IMG, 30, 45, 165, 45, $line_color );
header( "Content-type: image/png" );
imagepng($IMG);
imagecolordeallocate($IMG, $line_color );
imagecolordeallocate($IMG, $text_color );
imagecolordeallocate($IMG, $background );
imagedestroy($IMG);
exit;
?>
您也可以使用。它在性能方面可能比GD库更好
// Create new object
$im = new Imagick();
// Create new image with properties
$im->newImage( 1000, 1000, '#FF0000' );
// Write texts on it
$text_draw = new ImagickDraw();
$text_draw->setFont( 'path/to/font' );
$text_draw->setFontSize( 150 );
$text_draw->setStrokeColor('#fff');
$text_draw->setFillColor('#C0C0C0');
$im->setImageFormat( "png" );
$im->writeImage( 'path/to/save/filename.png' );
$img->destroy();
资料来源:嘿!那真是太酷了。我从来不知道我们能做到这一点
// Create new object
$im = new Imagick();
// Create new image with properties
$im->newImage( 1000, 1000, '#FF0000' );
// Write texts on it
$text_draw = new ImagickDraw();
$text_draw->setFont( 'path/to/font' );
$text_draw->setFontSize( 150 );
$text_draw->setStrokeColor('#fff');
$text_draw->setFillColor('#C0C0C0');
$im->setImageFormat( "png" );
$im->writeImage( 'path/to/save/filename.png' );
$img->destroy();