简体   繁体   中英

Rotate Image 90 degrees

I am able to upload the image to a web server and store the location in the database as follows:

/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:

$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:

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

You must create the image from imagecreatefromjpeg as follows

$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:

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

https://developer.mozilla.org/en-US/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:

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

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