SitePoint Sponsor |
|
User Tag List
Results 1 to 11 of 11
Thread: Creating images question
-
Aug 10, 2007, 03:28 #1
- Join Date
- Jun 2006
- Location
- My computer
- Posts
- 408
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Creating images question
I am wondering if someone could please tell me how I could create an image exactly like the attached one using PHP? Also I would like the image to be saved to the server. Thanks in advance.
-
Aug 10, 2007, 03:31 #2
- Join Date
- Feb 2005
- Location
- Birmingham, UK
- Posts
- 151
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
While waiting for the image to be approved, you will most probably have to use some form of th GD library, check out http://php.net/gd
Web Developer & Geek: hybridlogic.co.uk ~ lukelanchester.com
-
Aug 10, 2007, 04:28 #3
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
Using GD's own font selection:
PHP Code:<?php
header("Content-type: image/png");
$im = @imagecreate(200, 23)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 57, 122);
$text_color = imagecolorallocate($im, 255, 255, 255);
imagestring($im, 3, 10, 5, "ABOUT US", $text_color);
imagepng($im);
imagedestroy($im);
?>
play with the imagestring fonts and also look into imagettftext() for antialiased text.Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Aug 10, 2007, 06:07 #4
-
Aug 10, 2007, 12:25 #5
- Join Date
- Jun 2006
- Location
- My computer
- Posts
- 408
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks guys, you have been great. But what happens if I don't want to save the image as a png. I would prefer it to be saved as a gif or jpeg. My GD version is 2.0.28, if that helps.
-
Aug 10, 2007, 12:43 #6
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
Change the header to:
PHP Code:header("Content-type: image/jpeg");
imagepng ($im, "/pics/image.png")
PHP Code:imagejpeg ($im, "/pics/image.jpg")
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Aug 10, 2007, 12:58 #7
- Join Date
- Jun 2006
- Location
- My computer
- Posts
- 408
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I am now getting the following error:
<b>Warning</b>: imagejpeg() [<a href='function.imagejpeg'>function.imagejpeg</a>]: Unable to open '/images/aboutusbtn.jpeg' for writing in <b>/home/account/public_html/folder/createimage.php</b> on line <b>10</b><br />. Can someone please help me solve this error?
-
Aug 10, 2007, 13:00 #8
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
Post your code please
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Aug 10, 2007, 13:02 #9
- Join Date
- Jun 2006
- Location
- My computer
- Posts
- 408
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code PHP:ob_start(); header("Content-type: image/jpeg"); $im = @imagecreate(200, 23) or die("Cannot Initialize new GD image stream"); $background_color = imagecolorallocate($im, 0, 57, 122); $text_color = imagecolorallocate($im, 255, 255, 255); imagestring($im, 3, 10, 5, "ABOUT US", $text_color); imagejpeg ($im, "/images/aboutusbtn.jpeg"); // line 10 is here imagedestroy($im); ob_end_flush();
-
Aug 10, 2007, 13:29 #10
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
OK, using that code it works fine for me. The only thing I changed was the path it was saving to; I removed the /images/ and ran the code.
Check that the folder has write permissions and also - is this going to be used repeatedly for creating buttons on the fly? If so I would consider putting it into a function rather than call it through the browser.Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Aug 10, 2007, 18:12 #11
- Join Date
- Jun 2006
- Location
- My computer
- Posts
- 408
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have made more changes to the code and now it isn't displaying the text on the button. Also it is only saving it as .jpeg. Thanks in advance.
Code PHP:function makeimage() { $button = str_replace(' ', '', $pagetitle); header("Content-type: image/jpeg"); $im = @imagecreate(200, 23) or die("Cannot Initialize new GD image stream."); $background_color = imagecolorallocate($im, 0, 57, 122); $text_color = imagecolorallocate($im, 255, 255, 255); imagestring($im, 3, 10, 5, $button, $text_color); $button2 = imagejpeg ($im, $button.".jpeg"); imagedestroy($im); } makeimage();
Bookmarks