简体   繁体   中英

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

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

But anyway my task is to done only with 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"> . 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 . That's a specialized usage for which there exists a better function , 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".

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.

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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