ireallylikepie Posted April 28, 2013 Share Posted April 28, 2013 (edited) 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 April 28, 2013 by ireallylikepie Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/ Share on other sites More sharing options...
requinix Posted April 28, 2013 Share Posted April 28, 2013 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 "";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)... Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1426923 Share on other sites More sharing options...
ireallylikepie Posted April 28, 2013 Author Share Posted April 28, 2013 (edited) 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 April 28, 2013 by ireallylikepie Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1426970 Share on other sites More sharing options...
requinix Posted April 28, 2013 Share Posted April 28, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427033 Share on other sites More sharing options...
Barand Posted April 28, 2013 Share Posted April 28, 2013 (edited) 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 April 28, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427040 Share on other sites More sharing options...
ireallylikepie Posted April 29, 2013 Author Share Posted April 29, 2013 (edited) 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 April 29, 2013 by ireallylikepie Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427042 Share on other sites More sharing options...
ireallylikepie Posted April 29, 2013 Author Share Posted April 29, 2013 I've tried : imagesavealpha( $im, true ); and changing the code you've provided with: $im = imagerotate($im, $_GET['d'], 0); but it still doesn't seem to work. D: Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427044 Share on other sites More sharing options...
Barand Posted April 29, 2013 Share Posted April 29, 2013 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427046 Share on other sites More sharing options...
ireallylikepie Posted April 29, 2013 Author Share Posted April 29, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427047 Share on other sites More sharing options...
Barand Posted April 29, 2013 Share Posted April 29, 2013 that version gave me this Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427048 Share on other sites More sharing options...
ireallylikepie Posted April 29, 2013 Author Share Posted April 29, 2013 (edited) I must have something else wrong in my code then, because i see this: (Turquoise is the current background of the site) Edited April 29, 2013 by ireallylikepie Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427051 Share on other sites More sharing options...
Barand Posted April 29, 2013 Share Posted April 29, 2013 did you add this line imagecolortransparent($im, $bg); Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427052 Share on other sites More sharing options...
ireallylikepie Posted April 29, 2013 Author Share Posted April 29, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427053 Share on other sites More sharing options...
Barand Posted April 29, 2013 Share Posted April 29, 2013 is it better if you have $bg = imagecolorallocate($im, 0x40, 0xE0, 0xD0); // turquoise Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427055 Share on other sites More sharing options...
ireallylikepie Posted April 29, 2013 Author Share Posted April 29, 2013 Yes, however, I made it so that the user could choose the background color. Is it possible to use a variable in imagecolorallocate? Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427056 Share on other sites More sharing options...
Barand Posted April 29, 2013 Share Posted April 29, 2013 Pass it in the querystring like the image name and angle Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427075 Share on other sites More sharing options...
ireallylikepie Posted April 30, 2013 Author Share Posted April 30, 2013 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427200 Share on other sites More sharing options...
Barand Posted April 30, 2013 Share Posted April 30, 2013 Is $back a colour defined within the image's colour table by imagecolorallocate()? If not, then yes, you are doing something wrong Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427201 Share on other sites More sharing options...
ireallylikepie Posted April 30, 2013 Author Share Posted April 30, 2013 No, I created a textbox in which the user could input their desired background color. On mainpage.php, $back = $_GET['backgroundinput'] and then I print <body style='background-color:$back;'></body> Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427203 Share on other sites More sharing options...
Barand Posted April 30, 2013 Share Posted April 30, 2013 You need to break down $back into its $red, $green, $blue values then call $bg = imagecolorallocate($im, $red, $green, $blue); Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427211 Share on other sites More sharing options...
ireallylikepie Posted May 2, 2013 Author Share Posted May 2, 2013 I can't seem to get the variables from one php file to the other. :/ Should I be using include or $_GET ? Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427778 Share on other sites More sharing options...
Solution Barand Posted May 2, 2013 Solution Share Posted May 2, 2013 Same way as the other values that are being passed echo "<img src='rotate.php?i=$image&r=$degree&bg=$back' />"; Then use $_GET['bg'] Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427780 Share on other sites More sharing options...
ireallylikepie Posted May 2, 2013 Author Share Posted May 2, 2013 (edited) Ahh nevermind I've fixed it!(: Was missing the & Thank you so so so much for all your help! I truly appreciate it. Edited May 2, 2013 by ireallylikepie Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427846 Share on other sites More sharing options...
Barand Posted May 2, 2013 Share Posted May 2, 2013 src='rotate.php?i=$image&d=$degree;r=$r;b=$b;g=$g' params in the query string should be separated by ampersands, not semicolons Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427852 Share on other sites More sharing options...
ireallylikepie Posted May 3, 2013 Author Share Posted May 3, 2013 Yeah, haha, realized that shortly after replying to the post. (: Thanks again ! Quote Link to comment https://forums.phpfreaks.com/topic/277374-random-widthheight-and-random-rotation/#findComment-1427860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.