Working with images | save image to folder (directory)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Working with images | save image to folder (directory)

Post by hairytea »

Hi,

I have created a simple t-shirt designer in flash....basically when finished adding attributes etc you press a button and actionscript creates a jpeg from a screen capture of the image and sends this to my php file..

My php file finishes the image manipulation etc and open a page displaying the image. This is not what i want to happen...

I want the image to be saved to a directory for future manipulation.....

Could someone please tell me what code i need to...

A. name the image

and..

B. Save the image to a directory (./files/)

Code: Select all

 
<?php
 
error_reporting(0);
/**
 * Get the width and height of the destination image
 * from the POST variables and convert them into
 * integer values
 */
$w = (int)$_POST['width'];
$h = (int)$_POST['height'];
 
// create the image with desired width and height
 
$img = imagecreatetruecolor($w, $h);
 
// now fill the image with blank color
// do you remember i wont pass the 0xFFFFFF pixels 
// from flash?
imagefill($img, 0, 0, 0xFFFFFF);
 
$rows = 0;
$cols = 0;
 
// now process every POST variable which
// contains a pixel color
for($rows = 0; $rows < $h; $rows++){
    // convert the string into an array of n elements
    $c_row = explode(",", $_POST['px' . $rows]);
    for($cols = 0; $cols < $w; $cols++){
        // get the single pixel color value
        $value = $c_row[$cols];
        // if value is not empty (empty values are the blank pixels)
        if($value != ""){
            // get the hexadecimal string (must be 6 chars length)
            // so add the missing chars if needed
            $hex = $value;
            while(strlen($hex) < 6){
                $hex = "0" . $hex;
            }
            // convert value from HEX to RGB
            $r = hexdec(substr($hex, 0, 2));
            $g = hexdec(substr($hex, 2, 2));
            $b = hexdec(substr($hex, 4, 2));
            // allocate the new color
            // N.B. teorically if a color was already allocated 
            // we dont need to allocate another time
            // but this is only an example
            $test = imagecolorallocate($img, $r, $g, $b);
            // and paste that color into the image
            // at the correct position
            imagesetpixel($img, $cols, $rows, $test);
        }
    }
}
 
// print out the correct header to the browser
header("Content-type:image/jpeg");
// display the image
imagejpeg($img, "", 90);
 
?>
 
Many Many Manyt thanks in advance :D
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Working with images | save image to folder (directory)

Post by onion2k »

Read the PHP manual page for imagejpeg().
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Working with images | save image to folder (directory)

Post by hairytea »

i did and it says nothing at all about this??? :banghead:

thanks anyway
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Working with images | save image to folder (directory)

Post by onion2k »

hairytea wrote:i did and it says nothing at all about this??? :banghead:
I don't believe you. There's no way you could read the manual page and not see how to do this.
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: Working with images | save image to folder (directory)

Post by hairytea »

ok, i must be lting then...

this is what is on the php imagejpeg page...

imagejpeg
(PHP 4, PHP 5)

imagejpeg — Output image to browser or file

Description
bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )
imagejpeg() creates a JPEG file from the given image .

Parameters

image
An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.

To skip this argument in order to provide the quality parameter, use NULL.

quality
quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75).


What part of this describes what i am trying to do?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Working with images | save image to folder (directory)

Post by onion2k »

hairytea wrote:I want the image to be saved to a directory
The PHP Manual wrote:filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
Post Reply