Runescape Bits & Bytes
https://www.rsbandb.com/forums/

Editing a PSD file via PHP, and saving it.
https://www.rsbandb.com/forums/viewtopic.php?f=14&t=78231
Page 1 of 1

Author:  hummerwar921 [ January 29th, 2010, 5:03 pm ]
Post subject:  Editing a PSD file via PHP, and saving it.

I am looking to edit a PSD with a PHP script, like change a string of text, and then safe it as a PNG.

Kind of like how this site does their Runescape stat signatures.

Could anyone help? Thanks.

Author:  Adbot [ January 29th, 2010, 5:03 pm ]
Post subject:  Register and login to get these in-post ads to disappear


Author:  Chief Snake [ January 29th, 2010, 5:34 pm ]
Post subject:  Re: Editing a PSD file via PHP, and saving it.

I don't think you'd want to go down that road as it would be near impossible. The reason you can't see any special styles or blending options on text in our stat sigs (or the stat sigs of any other site for that matter) is because all of the dynamically generated text is added by the PHP GD library itself.

In other words we don't use PSDs. :P

Author:  hummerwar921 [ January 29th, 2010, 5:41 pm ]
Post subject:  Re: Editing a PSD file via PHP, and saving it.

Oh.

Well basically, I just want a background, and be able to generate text where I want it to go.

Would that be easily possible?

And thanks for the explanation. I haven't worked with the GD library much before.

Author:  Chief Snake [ January 30th, 2010, 12:22 am ]
Post subject:  Re: Editing a PSD file via PHP, and saving it.

Oh yep, that's quite possible.
If you're interested in adding effects to the texts there's not a lot that the GD library can do except add makeshift shadows by duplicating the text and moving it appropriately as shown below. If you don't want the shadow then just get rid of that one line.

This code outputs an entire image (I adapted this from code that I used for a blog signature) so it'll do exactly what you want it to.

Code:
<?php
// The lines below prevent the file from being cached (i.e. it is reloaded upon each view) and are currently commented - should only be uncommented if you are outputting dynamic information which will be regularly updated, and also if you don't mind using a lot of bandwidth on your host. You can use MySQL or an RSS grabber etc to output stuff dynamically.
#header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
#header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
#header("Cache-Control: no-store, no-cache, must-revalidate");
#header("Cache-Control: post-check=0, pre-check=0", false);
#header("Cache-Control: max-age=0");
#header("Pragma: no-cache");
header("Content-type: image/x-png"); // can use 'Content-type: image/jpeg/gif/x-png' instead (change output at end also)

// Load image template
$im = imagecreatefrompng("background.png"); // location of the background image (should also be imagecreatefromjpeg/gif if the file type is not png)
$textcolour = imagecolorallocate($im, 255, 255, 255); // colour of the text you're adding in an RGB value
$textshadow = imagecolorallocate($im, 0, 0, 0); // colour of the shadow on your text (if any) in an RGB value

// The following function gives text center alignment
function centerText($size, $angle, $font, $text, $centerPt){
   $bbox = imagettfbbox($size, $angle, $font, $text);
   $width = $bbox[2] - $bbox[0];
   $halfPt = $width / 2;
   $newX = $centerPt - $halfPt;
   return $newX;
}

// imagettftext ($im, font size, angle, location x-coord, location y-coord, font colour using imagecolorallocate() function, font file location, text);
// the centerText() function is a bit confusing to grasp but it goes in the x-coord parameter and you basically enter the same parameters into it as you did for the rest of the imagettftext() function. Read its parameters above to see what to enter.
// First apply a shadow (1px off the actual text), then actual text afterwards
$text = "This is the string of text you want to output";
imagettftext ($im, 20, 0, centerText(20, 0, './verdana.ttf', $text, 85) + 1, 87, $textshadow, "./verdana.ttf", $text);
imagettftext ($im, 20, 0, centerText(20, 0, './verdana.ttf', $text, 85), 86, $textcolour, "./verdana.ttf", $text);

// Throwing out the final image
imagepng($im);
// or
// imagepng($im);
// or
// imagejpeg($im,'',100);
// (change content-type in header also as necessary)

// Free the memory
imagedestroy($im);
?>

Author:  hummerwar921 [ January 30th, 2010, 2:44 am ]
Post subject:  Re: Editing a PSD file via PHP, and saving it.

Thank you very much, this code was helpful.

Problem resolved.

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/