TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   PHP GD query (http://www.talkphp.com/absolute-beginners/4036-php-gd-query.html)

Farmer 03-13-2009 04:04 AM

PHP GD query
 
I am trying to get the index color of a image but still no luck. I have 2 files.

-- image.php [GD generated image]
-- view.php [File to view the index color of image.php]

image.php

Code:

header("Content-Type: image/jpeg");

$img = imagecreate("200", "150");
$bgcolor = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
$text = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

imagejpeg($img);
imagedestroy($img)

view.php
Code:

$im = imagecreatefromjpeg('image.php');

// get a color
$start_x = 20;
$start_y = 20;
$color_index = imagecolorat($im, $start_x, $start_y);

// make it human readable
$color_tran = imagecolorsforindex($im, $color_index);

print_r($color_tran);

when i try other normal jpeg file it return values but when i try image.php file it'll not to return the value. Can you help me here.

Sakakuchi 03-14-2009 10:24 AM

Now thats just an assumption. But as far as I know imagecreatefromjpeg can only retrieve files with an .jpg and .jpeg ending. Thats why theres also a imagecreatefromJPEG -> for .JPG and .JPEG files.
Now, I might lie - but I think thats where you error occurs. I guess you can't fetch "jpg's" where the image is actually created by PHP/ -> or has an .php ending. But as I said - not sure whether I talk some bullshit here, just an theory...

ioan1k 03-15-2009 01:32 AM

This will not work as the GD library does not run the PHP script and only tries to load the JPEG image.

An alternative to this could be the following

PHP Code:

// image.php
$img imagecreate("200""150");
$bgcolor imagecolorallocate($imgmt_rand(0255), mt_rand(0255), mt_rand(0255));
$text imagecolorallocate($imgmt_rand(0255), mt_rand(0255), mt_rand(0255));

// Save image 
imagejpeg($img'testimage.jpeg');
imagedestroy($img);

// redirect to view
header('location: view.php'); 

PHP Code:

// view.php
// Redirect if there is no image
if (!file_exists('testimage.jpeg'))
{
    require(
'image.php');
}

// Load Image
$im imagecreatefromjpeg('testimage.jpeg');

// Delete image
@unlink('testimage.jpeg');
// get a color
$start_x 20;
$start_y 20;
$color_index imagecolorat($im$start_x$start_y);

// make it human readable
$color_tran imagecolorsforindex($im$color_index);

print_r($color_tran); 

Tested and works


All times are GMT. The time now is 07:35 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0