View Single Post
  #1  
Old 10-16-2012, 03:16 PM
PTGigi's Avatar
PTGigi PTGigi is offline
Crazily Friendly~HoF
 
Join Date: Jul 2009
Location: Somewhere >.>
Posts: 370
Gender: Female
Credits: 26,378
PTGigi
Default Editing Images with PHP

Hi, so there's no "save draft" option that I can find sooooooooo I'm posting this unfinished tutorial here. Sorry about that, but feel free to post on the little bit I've got thrown up here. Thanks~

Right, first note, anything I type is not going to be related to the MysidiaScript but might be cool things to edit into your script. This is because I do not have a Mys site at the moment. Eventually I will get one (probably next release or so) but for now, bear with my random findings.

BUT YES! Editing images with PHP. The most I've experimented with are merging two images and recoloring an image, so I'll talk about those for now. ;D

Now you may ask yourself, why would I do this? I have -insert art program here-, why do I want to use PHP? Well, this lets you edit and merge images on the fly, and can save you lots of time and space.

First, we'll start with the basics. Then I'll show you how merge images and recolor them~

For these examples I'll only be referring to PNGs and GIFs since those are most used for adoptables I'd think.

Defining an image:
First you need to decide which file type you're using and whether you're saving it to the server or not. Each has a basic set up like so:

GIF (not saved to server):

PHP Code:
<?php
 header
('Content-Type: image/gif');
 
$img imagecreatefromgif('imageurl');
  
 
 
 
imagegif($img);
 
imagedestroy($img);
 
?>

PNG (not saved to server):

PHP Code:
<?php
 header
('Content-Type: image/png');
 
$img imagecreatefrompng('imageurl');
  
 
 
 
imagepng($img);
 
imagedestroy($img);
 
?>
If you notice only two lines are changed between changing file types, the header (which tells the browser what to expect (an image)) and the function used to generate the image. You /can/ input a gif and convert it to a png or vice versa too. Use whatever output you'd like.

But when you save to the server there's a line you must remove and some specifications you need to add to the "imageTYPE" method:
PNG (saved to server):
PHP Code:
<?php
 $img 
imagecreatefrompng('imageurl');
 
$path="path/to/make/new/file.png";



 
$file imagepng($img,$path);
 
imagedestroy($img);
 echo 
"Image saved successfully! Saved image:<br /> <img src='".$path."' />";
 
?>
Like before, saving as a GIF if just as simple as changing "imagepng" to "imagegif". We take out the header because we want to echo out some text. We can then display the generated image because it now exists. The new parts we've added are simply putting the "imageTYPE" into a variable (so it can't throw out an image and give us an error) and adding the path where we want to save the image. Keep in mind if you make an image with the same name as another it'll save over it.

Right now this only outputs the image you just made. Which is boring...and pointless. SO NOW WE EDIT THEM!
__________________


"I see now that the circumstances of one's birth are irrelevant; it is what you do with the gift of life that determines who you are."~Mewtwo
My Adoptables|Nuzlocke Webcomic

Last edited by PTGigi; 10-18-2012 at 09:51 AM.
Reply With Quote