/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] GD Produces Gray JPEG Image

I’ve been trying to mess with GD to resize an image to fit an 800×600 resolution. I used an online tutorial I found, [url]http://www.phptutorial.info/learn/create_images/#code8[/url] — problem is, I get a grayed image, with like, 2 colors. Trying to resize [URL=”http://img71.imageshack.us/img71/4436/51200925424pmes9.jpg”]this image[/URL] will result in [URL=”http://img380.imageshack.us/img380/5425/1112hz8.jpg”]this one[/URL].
What am I doing wrong? Here’s the entire page code.

[code=php]<?php print_r($_FILES); echo ‘<br /><br />’;
if ($_FILES[‘avatar’][‘type’] != ‘image/jpeg’) echo ‘Wrong file format’;
$img[‘old’] = imagecreatefromjpeg($_FILES[‘avatar’][‘tmp_name’]);
$img[‘info’] = getimagesize($_FILES[‘avatar’][‘tmp_name’]);

print_r($img[‘info’]); echo ‘<br /><br />’;

$img[‘width’] = $img[‘info’][0];
$img[‘height’] = $img[‘info’][1];

$ratio = ($img[‘width’] > 800) ? (800 / $img[‘width’]) : 1;
$img[‘new_width’] = $img[‘width’] * $ratio;
$img[‘new_height’] = $img[‘height’] * $ratio;

$img[‘new’] = imagecreate($img[‘new_width’], $img[‘new_height’]);
imagecopyresized($img[‘new’], $img[‘old’], 0, 0, 0, 0, $img[‘new_width’], $img[‘new_height’], $img[‘width’], $img[‘height’]);
imagejpeg($img[‘new’], ‘uploads/111.jpg’, 100);
imagedestroy($img[‘new’]);

print_r($img);
?>
<img src=”<?=setting(‘absolute’);?>/uploads/111.jpg” />
<form method=”post” enctype=”multipart/form-data”>
<h2>Upload an avatar</h2>
<input type=”file” name=”avatar” /><br />
<input type=”submit” value=”Done” />
</form>[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Hen_AsrafauthorJan 06.2009 — Ok, I solved that problem (used createimagetruecolor instead of createimage), but now that I took it to the next step and created 2 copies of the same image with different resizing, something messes up. The first image turns out all black and the second turns out all gray. Here's the code...[code=php]<?php
if (isset($_FILES['avatar']))
{
$img['old']['img'] = imagecreatefromjpeg($_FILES['avatar']['tmp_name']);
$img['old']['info'] = getimagesize($_FILES['avatar']['tmp_name']);

$img['old']['width'] = $img['old']['info'][0];
$img['old']['height'] = $img['old']['info'][1];

$ratio = ($img['old']['width'] > setting('avatar_full_width')) ? (setting('avatar_full_width') / $img['old']['width']) : 1;
$img['new']['width'] = $img['old']['width'] * $ratio;
$img['new']['height'] = $img['old']['height'] * $ratio;

$img['new']['img'] = imagecreatetruecolor($img['new']['width'], $img['new']['height']);
$img['thumb']['img'] = imagecreatetruecolor(setting('avatar_width'), setting('avatar_width'));

imagecopyresized($img['new']['img'], $img['old']['img'], (setting('avatar_width') / 2) - ($img['old']['width'] / 2), 0, 0, 0, setting('avatar_width'), setting('avatar_width'), $img['old']['width'], $img['old']['height']);
imagecopyresized($img['thumb']['img'], $img['old']['img'], 0, 0, 0, 0, $img['new']['width'], $img['new']['height'], $img['old']['width'], $img['old']['height']);

imagejpeg($img['new']['img'], 'uploads/'.user('id').'.jpg', 100);
imagejpeg($img['thumb']['img'], 'uploads/'.user('id').'_thumb.jpg', 100);

imagedestroy($img['new']['img']);
imagedestroy($img['thumb']['img']);

print_r($img['new']);
print_r($img['old']);
}
?>
<img src="<?=setting('absolute');?>/uploads/<?=user('id');?>.jpg" />
<img src="<?=setting('absolute');?>/uploads/<?=user('id');?>_thumb.jpg" />
<form method="post" enctype="multipart/form-data">
<h2>Upload an avatar</h2>
<input type="file" name="avatar" /><br />
<input type="submit" value="Done" />
</form>[/code]
Copy linkTweet thisAlerts:
@Hen_AsrafauthorJan 06.2009 — Nvm, solved. I needed to switch the copyimageresized's parameters around cause I got them wrong, and then I had to fix the resizing sizes and positions algorithms.

...

I'm so proud of myself right now =D
×

Success!

Help @Hen_Asraf spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...