Jump to content

Random Width/Height and Random Rotation


ireallylikepie
Go to solution Solved by Barand,

Recommended Posts

Hi, 

New coder here! I'm making a php If loop that will post my image a set number of times (depending on user input in a text box), and I have that all figured out. However, I can't figure out how to rotate the image a random amount of degrees and have a random width/height for each image. Will rand generate new numbers for each time the image is echo-ed, or will it give the same number for all images?

 

This is what I have:

<?php

              function pic($picture) {

                        $degree = rand(0,360)

                        $num = rand(50,250)

                        $total = 0;
                        while ($total < $picture){
                            echo '<img src=blabla.gif width="$num" height="$num">';
                        $total = $total + 1;
                        }
                        
                    }
                        $picture = $_GET['input'];
                        $a = pic($picture);
                    }

?>

Thanks in advance!

Edited by ireallylikepie
Link to comment
Share on other sites

It will be a different amount. The problem is you're trying to put variables inside a single-quoted string. That won't work. Either use double-quotes

echo "blabla.gif";
or something else entirely.

 

CSS 3 is a very easy way to rotate an image but it requires browsers that support that. Many, perhaps most, do. That an option? Otherwise you'll have to rotate the image yourself (I mean with code, of course)...

Link to comment
Share on other sites

here is an example of rotating images.

Imports System.IO

Imports System.Drawing.Printing

Imports RasterEdge.Imaging

Imports RasterEdge.Imaging.Processing

 

Dim Image As New RasterEdgeImaging()

 

Public Sub RotateImage()

If True Then

Dim LoadImage As New Bitmap("C:\\1.bmp")

Dim rotate As Graphics = Graphics.FromImage(LoadImage)

rotate.TranslateTransform(CType(bmp.Width, Single) / 2, CType(bmp.Height, Single) / 2)

rotate.RotateTransform(rotationAngle)

rotate.TranslateTransform(-CType(bmp.Width, Single) / 2, -CType(bmp.Height, Single) / 2)

rotate.InterpolationMode = InterpolationMode.HighQualityBicubic

rotate.DrawImage(img, New Point(0, 0))

rotate.Dispose()

Return LoadImage()

End If

End Sub

Hmmm, I'm not sure what language this is, and how I would put it into my php loop. 

 

It will be a different amount. The problem is you're trying to put variables inside a single-quoted string. That won't work. Either use double-quotes

echo "<img src='blabla.gif' width='$num' height='$num'>";
or something else entirely.

 

CSS 3 is a very easy way to rotate an image but it requires browsers that support that. Many, perhaps most, do. That an option? Otherwise you'll have to rotate the image yourself (I mean with code, of course)...

 

 

Yay! It works now. (:

Hmm, but if I want each picture to rotate a different number of degrees, wouldn't I have to put it into the php loop? Could I use:

echo file_get_contents('picture.css');

And then in the separate css file would I put in:

.image-box img { 
   transform: rotate($degree);
   -moz-transform: rotate($degree);
   -webkit-transform: rotate($degree);
   }

I'm not sure if you can put in variables in CSS, or how to put something in php under a class. :/

Edited by ireallylikepie
Link to comment
Share on other sites

No, you can't put variables in CSS. Not without running the file as a PHP script.

 

Are you rotating all the images by the same amount? Then I would put the CSS in a

Link to comment
Share on other sites

Alternative approach

 

main page

<?php
$degree = 45;
$image = 'images/logo.PNG';

echo "<img src='rotate.php?i=$image&r=$degree' />"
?>

rotate.php

<?php
    $im = imagecreatefrompng($_GET['i']);
    $bg = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
    $im = imagerotate($im, $_GET['r'], $bg);
    header('content-type: image/png');
    imagepng($im);
    imagedestroy($im);
?>
Edited by Barand
Link to comment
Share on other sites

 

Alternative approach

 

main page

<?php$degree = 45;$image = 'images/logo.PNG';echo "<img src='rotate.php?i=$image&r=$degree' />"?>
rotate.php
<?php    $im = imagecreatefrompng($_GET['i']);    $bg = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);    $im = imagerotate($im, $_GET['r'], $bg);    header('content-type: image/png');    imagepng($im);    imagedestroy($im);?>

The rotating worked!

However, my images were basically all in black with a transparent background, but for some reason the rotating cause there to be a white background for each image. I'm guessing that this is from the image colorallocate... so is there someway I can make do without it ?

No, you can't put variables in CSS. Not without running the file as a PHP script.

 

Are you rotating all the images by the same amount? Then I would put the CSS in a <style> in the <head>. But the code you posted has a different amount for each image, in which case I'd say to use inline CSS for each. Blasphemy, I know.

No, I would like each image to rotate by a randomly generated degree. So you're saying I could use a <div style = ""></div> ? Edited by ireallylikepie
Link to comment
Share on other sites

 

try this for rotate.php

<?php
    $im = imagecreatefrompng($_GET['i']);
    $bg = imagecolorallocate($im, 0xFF, 0x00, 0xFF);
    $im = imagerotate($im, $_GET['r'], $bg);
    imagecolortransparent($im, $bg);
    header('content-type: image/png');
    imagepng($im);
    imagedestroy($im);
?>

Hmm... now it's pink?

Link to comment
Share on other sites

Yes. I think I know what the problem is though.

I added these two lines as well, to keep the original image background transparent.

    imagealphablending( $im, false );
    imagesavealpha( $im, true );

When I remove those two lines, the image background isn't transparent, but the pink corners are gone.

Link to comment
Share on other sites

Pass it in the querystring like the image name and angle

Ahhh! Am I doing something wrong --- now no image shows up!!

<?php
    $im = imagecreatefrompng($_GET['i']);
    include 'mainpage.php';
    $bg = $back;
    $im = imagerotate($im, $_GET['r'], $bg);
    header('content-type: image/png');
    imagecolortransparent($im, $bg);
    imagealphablending( $im, false );
    imagesavealpha( $im, true );
    imagepng($im);
    imagedestroy($im);
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.