簡體   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