在同一页面上旋转图像

时间:2014-06-16 19:14:22

标签: php

我想从一个位置旋转上传和检索的图像。是的,我差不多完成了。但问题是,由于标题(“content-type:image / jpeg”)页面被重定向到另一个/或图像格式。我想在与原始图像相同的页面中显示它。这里是我的代码..

     $imgnames="upload/".$_SESSION["img"];
     header("content-type: image/jpeg");
     $source=imagecreatefromjpeg($imgnames);
     $rotate=imagerotate($source,$degree,0); 
     imagejpeg($rotate);

我也使用了css属性。

   echo "<img src='$imgnames' style='image-orientation:".$degree."deg;' />";

但无论如何我的任务是只用php完成。请指导我,或提供你的任何参考

感谢提前。

2 个答案:

答案 0 :(得分:0)

您需要单独生成图像 - 例如<img src="path/to/image.php?id=123">。试图将它用作这样的变量是行不通的。

答案 1 :(得分:0)

<?php
    // Okay, so in your upload page 
    $imgName  = "upload/".$_SESSION["img"];
    $source=imagecreatefromjpeg($imgName);
    $rotate=imagerotate($source, $degree,0); 


    // you generate  a PHP uniqid, 
    $uniqid = uniqid();

    // and use it to store the image

    $rotImage = "upload/".$uniqid.".jpg";

    // using imagejpeg to save to a file; 

    imagejpeg($rotate, $rotImage, $quality = 75);

    // then just output a html containing ` <img src="UniqueId.000.jpg" />` 
    // and another img tag with the other file.

    print <<<IMAGES
       <img src="$imgName" />
       <img src="$rotName" />
IMAGES;

    // The browser will do the rest.
?>

更新

实际上,虽然uniqid()通常有效,但我们希望使用uniqid() 来创建文件。这是there exists a better functiontempnam()

的专门用法

然而,tempnam()不允许指定自定义扩展,许多浏览器不愿意下载名为“foo”而不是“foo.jpg”的JPEG文件。

为了更加确定我们可以使用两个相同的唯一名称

    $uniqid = uniqid('', true);

添加“true”参数以使更长的名称具有更多的熵。

否则我们需要一个更灵活的功能来检查是否已存在唯一名称,如果是,则生成另一个名称:而不是

    $uniqid = uniqid();
    $rotImage = "upload/".$uniqid.".jpg";

我们使用

    $rotImage = uniqueFile("upload/*.jpg");

其中uniqueFile()

function uniqueFile($template, $more = false) {
    for ($retries = 0; $retries < 3; $retries++) {
        $testfile = preg_replace_callback(
             '#\\*#',                          // replace asterisks
             function() use($more) { 
                 return uniqid('', $more);     // with unique strings
             },
             $template                         // throughout the template
        );
        if (file_exists($testfile)) {
            continue;
        }
        // We don't want to return a filename if it has few chances of being usable
        if (!is_writeable($dir = dirname($testfile))) {
            trigger_error("Cannot create unique files in {$dir}", E_USER_ERROR);
        }
        return $testfile;
    }
    // If it doesn't work after three retries, something is seriously broken.
    trigger_error("Cannot create unique file {$template}", E_USER_ERROR);
}
相关问题