简体   繁体   English

旋转同一页面上的图像

[英]Rotate an image on same page

I want to rotate an uploaded and retrieved image from one location. 我想从一个位置旋转上传和检索的图像。 Yes i am almost done. 是的,我快完成了。 But the problem is, due to header("content-type: image/jpeg") the page redirected to another/or image format. 但是问题是,由于页眉(“ content-type:image / jpeg”),页面被重定向到另一种或图像格式。 I want to display it in same page as original image in. Here my code.. 我想在与原始图像相同的页面中显示它。

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

i also did with css property. 我也做过CSS属性。

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

But anyway my task is to done only with php. 但是无论如何,我的任务是只用php完成。 Please guide me, or give any reference you have 请指导我,或给您任何参考

thanks advance. 谢谢提前。

You need to generate the image separately - something like <img src="path/to/image.php?id=123"> . 您需要单独生成图像-类似于<img src="path/to/image.php?id=123"> Trying to use it as a variable like that isn't going to work. 试图将其用作这样的变量是行不通的。

<?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.
?>

UPDATE 更新

Actually, while uniqid() usually works, we want to use uniqid() to create a file . 实际上,虽然uniqid()通常可以工作,但我们想使用uniqid() 创建文件 That's a specialized usage for which there exists a better function , tempnam() . 这是一种专门用途,为此存在一个更好的函数 tempnam()

Yet, tempnam() does not allow a custom extension to be specified, and many browsers would balk at downloading a JPEG file called "foo" instead of "foo.jpg". 但是, tempnam()不允许指定自定义扩展名,许多浏览器会讨厌下载名为“ foo”而不是“ foo.jpg”的JPEG文件。

To be more sure that there will not be two identical unique names we can use 为了确保没有两个相同的唯一名称,我们可以使用

    $uniqid = uniqid('', true);

adding the "true" parameter to have a longer name with more entropy. 添加“ true”参数,使其具有更长的名称和更多的熵。

Otherwise we need a more flexible function that will check if a unique name already exists and, if so, generate another: instead of 否则,我们需要一个更灵活的函数,该函数将检查是否已经存在唯一名称,如果存在,则生成另一个:

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

we use 我们用

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

where uniqueFile() is 在哪里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