簡體   English   中英

將影像旋轉90度

[英]Rotate Image 90 degrees

我可以將圖像上傳到Web服務器,並將位置存儲在數據庫中,如下所示:

/123cb2kbls.62452152.jpg

然后,我可以檢索文件的位置並將其顯示到Web瀏覽器,而不會出現以下問題:

$row['pic_loc'] // has a value of '/123cb2kbls.62452152.jpg'

while($row = mysqli_fetch_array($result)) {
    $width = 900;
    $display_height = $width*$row["height_ratio"];
    $source = $row["pic_loc"];

    echo "<p><td><img src='" . $source     ."' width=$width height=$display_height></p></td>";          
}

我的挑戰是,我無法使用while()循環中的以下代碼來使imagerotate()將圖像翻轉90度:

$rotate90 = imagerotate($source,90,0);
echo "<p><td><img src='" . $rotate90   ."' width=$display_height height=$width></p></td>";

唯一發生的是,我看到了已旋轉90度的文件的輪廓,但是沒有圖像顯示。

您必須按照以下步驟從imagecreatefromjpeg創建圖像

$imurl = "../images/sample.jpg";
$file = imagecreatefromjpeg($imurl); //http://nz2.php.net/manual/en/function.imagecreatefromjpeg.php
$rotim = imagerotate($file, 90, 0);   //http://nz2.php.net/manual/en/function.imagerotate.php      
imagejpeg($rotim, $imurl);

從您擁有的內容來看(我可能是錯的) ,我認為也許使用CSS而不是使用大量服務器功能會更好,尤其是如果您不保存圖像的永久旋轉:

<style>
.pic-loc-rotate img {
    transform: rotate(90deg);
}
</style>

<?php
while($row = mysqli_fetch_array($result)):
    $width  = 900;
    $height = ($width * $row["height_ratio"]) ?>

    <td class="pic-loc-rotate">
        <p><img src="<?php echo $row["pic_loc"] ?>" width="<?php echo $width ?>" height="<?php echo $height ?>" /></p>
    </td>

<?php endwhile ?>

通過CSS旋轉:

https://developer.mozilla.org/zh-CN/docs/Web/CSS/transform-function/rotate


如果您確實希望通過服務器旋轉圖像(但不想存儲它們),則可以使用輸出緩沖區和base64_encode() ,但是CSS絕對是更好的解決方案:

<?php
while($row = mysqli_fetch_array($result)):
    # Start the buffer so the image doesn't output to the browser yet
    ob_start();
    # Fetch your image and rotate it
    # Create to the buffer
    imagejpeg(imagerotate(imagecreatefromjpeg($row["pic_loc"]), 90, 0));
    # Encode the contents of the output buffer to base64
    $imageBase64    =   base64_encode(ob_get_contents());
    # Clear buffer
    ob_end_clean();
    # Do your other stuff
    $width  = 900;
    $height = ($width * $row["height_ratio"]) ?>

        <td class="pic-loc-rotate">
            <p><img src="data:image/jpeg;base64,<?php echo $imageBase64 ?>" width="<?php echo $width ?>" height="<?php echo $height ?>" /></p>
        </td>

<?php endwhile ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM