繁体   English   中英

旋转同一页面上的图像

[英]Rotate an image on same page

我想从一个位置旋转上传和检索的图像。 是的,我快完成了。 但是问题是,由于页眉(“ 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完成。 请指导我,或给您任何参考

谢谢提前。

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

<?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() 创建文件 这是一种专门用途,为此存在一个更好的函数 tempnam()

但是, 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM