如何在PHP中旋转图像?

4
我有一个函数,可以生成适合圆形底部的文本。由于我不知道是否可以用其他方式使这个函数适应圆形顶部,并面向我,因此我想旋转图像,写上它,再将其旋转回来,然后再次写上它。如何在不更改图像名称的情况下实现这一点?
我尝试过类似以下的内容:
<?php
function  create_image()
{

$im = @imagecreate(140, 140)or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);

imageellipse ( $im , $cx , $cy , $size*2 , $size*2 , $black );

write($im,$cx,$cy,$size,$s,$e,$black,$text1,$font,$size,$pad);
imagerotate($im, 180,0);
write($im,$cx,$cy,$size,$s,$e,$black,$text2,$font,$size,$pad);
imagerotate($im, 180,0);

imagepng($im,"image.png");
imagedestroy($im);  

}
?>

<?php
create_image();
print "<img src=image.png?".date("U").">";
?>

但是它不起作用。它不能旋转图像。

你能帮我吗?

谢谢!


你从哪里得到这个脚本的?它不完整,有错误...而且你调用了未知的函数,比如write。很明显这只是复制粘贴...为什么不详细解释你想要什么呢? - Baba
@Baba我认为write函数与这个想法无关。我需要学习如何在PHP中旋转图像...而不改变它的名称... - Cristina Ursu
为什么需要旋转两次? - Baba
试一下 http://php.net/manual/zh/function.imagerotate.php - Tarek
3个回答

4
为什么不使用普通图片并添加一些CSS呢?
CSS
.yourImage {
 -webkit-transform: rotate(-90deg);
 -moz-transform: rotate(-90deg);
 filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
}

HTML

<img class="yourImage" src="originalImage.jpg">

因为这是CSS3,不适用于所有浏览器,我需要将图像旋转两次。而且我需要使用PHP来完成它... - Cristina Ursu

3

不确定为什么需要旋转两次...但这是您的代码应该看起来像的样子

function create_image($img) {
    $im = @imagecreatefrompng($img) or die("Cannot Initialize new GD image stream");
    $rotate = imagerotate($im, 180, 0);
    imagepng($rotate);
    imagedestroy($rotate);
    imagedestroy($im);
}

header('Content-Type: image/png');
$image = "a.png";
create_image($image);

谢谢!好的,我需要旋转两次,因为写函数只能在圆的下部写文本... - Cristina Ursu

2
您可以使用php的旋转函数轻松实现。下面是简单的代码:

你可以使用php的旋转函数轻松实现。这里是简单的代码:

<?php 
 $image = 'test.jpg';

// The file you are rotating

//How many degrees you wish to rotate
 $degrees = 180;

// This sets the image type to .jpg but can be changed to png or gif
 header('Content-type: image/jpeg') ;

// Create the canvas
   $src = $image;
$system = explode(".", $src);

if (preg_match("/jpg|jpeg/", $system[1]))
{
    $src_img=imagecreatefromjpeg($src);
}
if (preg_match("/png/", $system[1]))
{
    $src_img = imagecreatefrompng($src);
}
if (preg_match("/gif/", $system[1]))
{
    $src_img = imagecreatefromgif($src);
}

  // Rotates the image
  $rotate = imagerotate($src_img, $degrees, 0) ;

  // Outputs a jpg image, you could change this to gif or png if needed
  if (preg_match("/png/", $system[1]))
{
    imagepng($rotate,$image); 
} 
else if (preg_match("/gif/", $system[1]))
{
    imagegif($rotate, $image);
}
else
{
    imagejpeg($rotate, $image); 
}

imagedestroy($rotate);  
imagedestroy($src_img);

   ?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接