简体   繁体   English

将影像旋转90度

[英]Rotate Image 90 degrees

I am able to upload the image to a web server and store the location in the database as follows: 我可以将图像上传到Web服务器,并将位置存储在数据库中,如下所示:

/123cb2kbls.62452152.jpg

Then, I am able to retrieve the location of the file and display it to the web browser with no problem as follows: 然后,我可以检索文件的位置并将其显示到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>";          
}

My challenge is that I cannot get the imagerotate() to flip the image 90 degrees using the following code inside my while() loop: 我的挑战是,我无法使用while()循环中的以下代码来使imagerotate()将图像翻转90度:

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

The only thing that happens is that I see the outline of a file that has been rotated 90-degrees, but no image displays. 唯一发生的是,我看到了已旋转90度的文件的轮廓,但是没有图像显示。

You must create the image from imagecreatefromjpeg as follows 您必须按照以下步骤从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);

Judging from what you have (and I could be wrong) , I think maybe css would be better for this instead of using a bunch of server power, especially if you are not saving a permanent rotation to the images: 从您拥有的内容来看(我可能是错的) ,我认为也许使用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 ?>

Rotate via CSS: 通过CSS旋转:

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


If you really want to have the images rotated via the server (but don't want to store them), you can use the output buffer and base64_encode() , but CSS is definitely a better solution: 如果您确实希望通过服务器旋转图像(但不想存储它们),则可以使用输出缓冲区和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