Thread View: alt.comp.lang.php
3 messages
3 total messages
Started by TheXhippy
Mon, 07 Oct 2002 16:10
Issues with ImageGIF and ImageJPEG functions
Author: TheXhippy
Date: Mon, 07 Oct 2002 16:10
Date: Mon, 07 Oct 2002 16:10
82 lines
4256 bytes
4256 bytes
Hello,
I'm building a system where a user can upload an image to my server via an
html form like this:
<FORM NAME="add_img" METHOD="post" ACTION="showpic.php"
ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="image">
<INPUT TYPE="submit" NAME="Submit" VALUE="Add New Property">
</FORM>
the file it posts to looks like this:
<?php
$store_dir= "../photo/";
if(strlen($HTTP_POST_FILES['image']['name'])>0)
{
$tmp_array = explode(".", $HTTP_POST_FILES['image']['name']);
$tmp_length = count($tmp_array);
$extn = strtolower($tmp_array[$tmp_length-1]);
$store_name = 'resized.'.$extn;
@copy($HTTP_POST_FILES['image']['tmp_name'],$store_dir.$store_name);
@unlink($HTTP_POST_FILES['image']['tmp_name']);
$size=GetImageSize($store_dir.$store_name);
$max_width = 300;
$max_height = 200;
$width_ratio = ($size[0] / $max_width);
$height_ratio = ($size[1] / $max_height);
$width_ratio>=$height_ratio ? $ratio = $width_ratio : $ratio =
$height_ratio;
$new_width = ($size[0] / $ratio);
$new_height = ($size[1] / $ratio);
$resized_image = imagecreate($new_width,$new_height);
switch($extn)
{
case 'gif':
$src_img = imagecreatefromgif($store_dir.$store_name);
imagecopyresampled($resized_image, $src_img, 0,0,0,0,
$new_width,$new_height,$size[0],$size[1]);
imagegif($resized_image,$store_dir.$store_name);
break;
case 'jpg':
$src_img = imagecreatefromjpeg($store_dir.$store_name);
imagecopyresampled($resized_image, $src_img, 0,0,0,0,
$new_width,$new_height,$size[0],$size[1]);
imagejpeg($resized_image,$store_dir.$store_name);
break;
}
}
print('<IMG SRC="'.$store_dir.$store_name.'"><BR>');
?>
This takes the uploaded image and resizes it to fit within a maximum
bounding box of 300x200 pixels but keeps the originals aspect ratio and it
all works fine except that:
1) It trashes the colours on jpegs so they look like 16 colour posterised
crap
and
2) It creates gifs that my browser just won't show on screen - the file is
there I can ftp it down and view it in Photoshop but it shows broken image
on the screen.
So what am I doing wrong? I assume there is a default colour depth of an
image created using imagecreate() and it seems to be the standard first 16
and I assume therefore I need to change this before I go
imagecopyresampled() or imagejpeg() but I cannot find any info on this.
Using
GD > 2
PHP 4.06
linux kernel 2.4.18
apache 1.3.23
Thanks in advance
TheXhippy
Re: Issues with ImageGIF and ImageJPEG functions
Author: NoT GorillA
Date: Mon, 07 Oct 2002 17:33
Date: Mon, 07 Oct 2002 17:33
94 lines
4553 bytes
4553 bytes
"TheXhippy" <thexhippy@notmail.com> wrote in message
news:ansblo$88m$1@paris.btinternet.com...
> Hello,
>
> I'm building a system where a user can upload an image to my server via an
> html form like this:
>
> <FORM NAME="add_img" METHOD="post" ACTION="showpic.php"
> ENCTYPE="multipart/form-data">
> <INPUT TYPE="file" NAME="image">
> <INPUT TYPE="submit" NAME="Submit" VALUE="Add New Property">
> </FORM>
>
> the file it posts to looks like this:
>
> <?php
> $store_dir= "../photo/";
>
> if(strlen($HTTP_POST_FILES['image']['name'])>0)
> {
> $tmp_array = explode(".", $HTTP_POST_FILES['image']['name']);
> $tmp_length = count($tmp_array);
> $extn = strtolower($tmp_array[$tmp_length-1]);
> $store_name = 'resized.'.$extn;
> @copy($HTTP_POST_FILES['image']['tmp_name'],$store_dir.$store_name);
> @unlink($HTTP_POST_FILES['image']['tmp_name']);
> $size=GetImageSize($store_dir.$store_name);
> $max_width = 300;
> $max_height = 200;
> $width_ratio = ($size[0] / $max_width);
> $height_ratio = ($size[1] / $max_height);
> $width_ratio>=$height_ratio ? $ratio = $width_ratio : $ratio =
> $height_ratio;
> $new_width = ($size[0] / $ratio);
> $new_height = ($size[1] / $ratio);
> $resized_image = imagecreate($new_width,$new_height);
> switch($extn)
> {
> case 'gif':
> $src_img = imagecreatefromgif($store_dir.$store_name);
> imagecopyresampled($resized_image, $src_img, 0,0,0,0,
> $new_width,$new_height,$size[0],$size[1]);
> imagegif($resized_image,$store_dir.$store_name);
> break;
>
> case 'jpg':
> $src_img = imagecreatefromjpeg($store_dir.$store_name);
> imagecopyresampled($resized_image, $src_img, 0,0,0,0,
> $new_width,$new_height,$size[0],$size[1]);
> imagejpeg($resized_image,$store_dir.$store_name);
> break;
> }
> }
> print('<IMG SRC="'.$store_dir.$store_name.'"><BR>');
> ?>
>
>
> This takes the uploaded image and resizes it to fit within a maximum
> bounding box of 300x200 pixels but keeps the originals aspect ratio and it
> all works fine except that:
>
> 1) It trashes the colours on jpegs so they look like 16 colour posterised
> crap
>
> and
>
> 2) It creates gifs that my browser just won't show on screen - the file is
> there I can ftp it down and view it in Photoshop but it shows broken image
> on the screen.
>
> So what am I doing wrong? I assume there is a default colour depth of an
> image created using imagecreate() and it seems to be the standard first 16
> and I assume therefore I need to change this before I go
> imagecopyresampled() or imagejpeg() but I cannot find any info on this.
>
> Using
> GD > 2
> PHP 4.06
> linux kernel 2.4.18
> apache 1.3.23
>
> Thanks in advance
>
> TheXhippy
>
>
>
don't you need an imagecreatetruecolor() in there?
--
NoT
I'd rather not take sides until I hear the monkey's version.
Re: Issues with ImageGIF and ImageJPEG functions
Author: Paskal van Lomm
Date: Mon, 07 Oct 2002 18:25
Date: Mon, 07 Oct 2002 18:25
4 lines
1527 bytes
1527 bytes
look at my effort for the same: www.phpdpa.com and www.phpdpa.com/example. maybe someting helpfull in the programm files paskal
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads