Php 使用GD将图像旋转90度
我试图生成一些文本,并将整个图像旋转90度,而不是单个字母。它必须是一个透明的PNG,并且不使用现有图像作为基础Php 使用GD将图像旋转90度,php,gd,Php,Gd,我试图生成一些文本,并将整个图像旋转90度,而不是单个字母。它必须是一个透明的PNG,并且不使用现有图像作为基础 谢谢这是使用GD lib旋转图像的最佳方法 $img = new Imagick('image.png'); $img->rotateImage( new ImagickPixel('none'), 7 ); $img->trimImage ( 0 ); $img->writeImage('rotateImage.png'); $img->dest
谢谢这是使用GD lib旋转图像的最佳方法
$img = new Imagick('image.png');
$img->rotateImage( new ImagickPixel('none'), 7 );
$img->trimImage ( 0 );
$img->writeImage('rotateImage.png');
$img->destroy();
尝试一下这个脚本
define("DEFAULT_FONT", "fonts/BebasNeue-webfont.ttf");
define("DEFAULT_COLOR", "ed217c");
define("DEFAULT_SIZE", 24);
define("DEFAULT_ANGLE", 0);
define("DEFAULT_PADDING", 10);
define("DEFAULT_SPACING", 0);
$text = $_GET['text'];
if(isset($_GET['transform'])):
switch ($_GET['transform']){
case 'uc':
$text = strtoupper($_GET['text']);
break;
case 'lc':
$text = strtolower($_GET['text']);
break;
case 'ucf':
$text = ucfirst($_GET['text']);
break;
case 'ucw':
$text = ucwords($_GET['text']);
break;
}
endif;
$font = $_GET['font'] ? $_GET['font'] : DEFAULT_FONT;
$color = (strlen($_GET['color']) == 6 && ctype_alnum($_GET['color'])) ? "0x" . $_GET['color'] : "0x" . DEFAULT_COLOR;
$size = (is_numeric($_GET['size'])) ? $_GET['size'] : DEFAULT_SIZE;
$angle = (is_numeric($_GET['angle'])) ? $_GET['angle'] : DEFAULT_ANGLE;
$padding = (is_numeric($_GET['padding'])) ? $_GET['padding']*4 : DEFAULT_PADDING*4;
$spacing = (is_numeric($_GET['spacing'])) ? $_GET['spacing'] : DEFAULT_SPACING;
$text_dimensions = calculateTextDimensions($text, $font, $size, $angle, $spacing);
$image_width = $text_dimensions["width"] + $padding;
$image_height = $text_dimensions["height"] + $padding;
$new_image = imagecreatetruecolor($image_width, $image_height);
ImageFill($new_image, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($new_image, true);
imagealphablending($new_image, false);
imagettftextSp($new_image, $size, $angle, $text_dimensions["left"] + ($image_width / 2) - ($text_dimensions["width"] / 2), $text_dimensions["top"] + ($image_height / 2) - ($text_dimensions["height"] / 2), $color, $font, $text, $spacing);
imagepng($new_image);
imagerotate($new_image, 90, 0);
imagedestroy($new_image);
function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0)
{
if ($spacing == 0)
{
imagettftext($image, $size, $angle, $x, $y, $color, $font, $text);
}
else
{
$temp_x = $x;
for ($i = 0; $i < strlen($text); $i++)
{
$bbox = imagettftext($image, $size, $angle, $temp_x, $y, $color, $font, $text[$i]);
$temp_x += $spacing + ($bbox[2] - $bbox[0]);
}
}
}
function calculateTextDimensions($text, $font, $size, $angle, $spacing) {
$rect = imagettfbbox($size, $angle, $font, $text);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$spacing = ($spacing*(strlen($text)+2));
return array(
"left" => abs($minX) - 1,
"top" => abs($minY) - 1,
"width" => ($maxX - $minX)+$spacing,
"height" => $maxY - $minY,
"box" => $rect
);
}
header("content-type: image/png");
define(“默认字体”、“字体/BebasNeue-webfont.ttf”);
定义(“默认颜色”、“ed217c”);
定义(“默认大小”,24);
定义(“默认角度”,0);
定义(“默认填充”,10);
定义(“默认_间距”,0);
$text=$_GET['text'];
如果(isset($\u GET['transform']):
开关($\u GET['transform']){
“uc”案例:
$text=strtoupper($_GET['text']);
打破
“信用证”案件:
$text=strtolower($_GET['text']);
打破
案例“ucf”:
$text=ucfirst($_GET['text']);
打破
案例“ucw”:
$text=ucwords($_GET['text']);
打破
}
endif;
$font=$\u获取['font']$_获取['font']:默认字体;
$color=(strlen($\u GET['color'])==6&&ctype\u alnum($\u GET['color']))?“0x”$_获取['color']:“0x”。默认颜色;
$size=(是数字($\u GET['size'])$_获取['size']:默认的大小;
$angle=(是数字($\u GET['angle'])$_获取['angle']:默认角度;
$padding=(是数字($\u GET['padding'])$_GET['padding']*4:默认的_padding*4;
$spating=(是数字($\u GET['spating'])$_获取['spacing']:默认的_间距;
$text\u dimensions=calculateTextDimensions($text、$font、$size、$angle、$space);
$image\u width=$text\u维度[“width”]+$padding;
$image\u height=$text\u维度[“height”]+$padding;
$new\u image=ImageCreateTureColor($image\u width,$image\u height);
ImageFill($new_image,0,0,IMG_COLOR_TRANSPARENT);
imagesavealpha($new_image,true);
imagealphablending($new_image,false);
imagettftextSp($new_image、$size、$angle、$text_dimensions[“left”]+($image_width/2)-($text_dimensions[“width”]/2)、$text_dimensions[“top”]+($image_height/2)-($text_dimensions[“height”]/2)、$color、$font、$text、$space);
imagepng($new_image);
imagerotate($new_image,90,0);
imagedestroy($new_image);
函数imagettftextSp($image、$size、$angle、$x、$y、$color、$font、$text、$spacing=0)
{
如果($spating==0)
{
imagettftext($image、$size、$angle、$x、$y、$color、$font、$text);
}
其他的
{
$temp_x=$x;
对于($i=0;$iabs($minX)-1,
“顶部”=>abs($minY)-1,
“宽度”=>($maxX-$minX)+$spacing,
“高度”=>$maxY-$minY,
“box”=>$rect
);
}
标题(“内容类型:图像/png”);
可以通过IMG SRC标记将旋转角度传递给脚本。我在Wordpress安装中使用了这个,但是我要生成的代码如下所示
<img src="<?php echo get_template_directory_uri(); ?>/imgCreate.php?text=<?php echo $page->post_title; ?>&font=&transform=&size=45&angle=90&color=&padding=18&spacing=0" />
/imgCreate.php?text=&font=&transform=&size=45&angle=90&color=&padding=18&space=0/>
使用gd库中的imagerotate()
:
$img = imagerotate($img, 90, 0);
第一个参数是图像源,即来自imagecreatefrompng()
第二个参数是度
第三个参数是背景色
查看更多关于Nice的信息,以及您希望从我们这里得到什么?。。?以及到目前为止您尝试了什么?使用gd。这是CSS。@putvande-我相信这是答案。PNG是透明的,而不是JPG这是imagemagick,不是gd…涵盖了所有要求和一些要求。任何未使用的内容都将默认为顶部的定义。唯一需要注意的是旋转不接受负值,所以-90应该是270。OP没有指定顺时针或逆时针90度。