🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.php
Article #12

Issues with ImageGIF and ImageJPEG functions

#12
From: TheXhippy
Date: Mon, 07 Oct 2002 16:10
82 lines
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
Technical Details
Message-ID: <ansblo$88m$1@paris.btinternet.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!feed.news.nacamar.de!newsfeed.stueberl.de!newsr1.ipcore.viaginterkom.de!btnet-peer1!btnet-peer0!btnet-feed5!btnet!news.btopenworld.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f9981,b8cc33b4c63e8aa8,start
X-Google-Attributes: gidf9981,public
X-Google-Thread: 1107bb,b8cc33b4c63e8aa8,start
X-Google-Attributes: gid1107bb,public
X-Google-Thread: 10701e,b8cc33b4c63e8aa8,start
X-Google-Attributes: gid10701e,public
X-Google-Thread: 10ce1c,b8cc33b4c63e8aa8,start
X-Google-Attributes: gid10ce1c,public
X-Google-Thread: 102e3b,b8cc33b4c63e8aa8,start
X-Google-Attributes: gid102e3b,public
X-Google-ArrivalTime: 2002-10-07 09:11:04 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!feed.news.nacamar.de!newsfeed.stueberl.de!newsr1.ipcore.viaginterkom.de!btnet-peer1!btnet-peer0!btnet-feed5!btnet!news.btopenworld.com!not-for-mail
From: "TheXhippy" <thexhippy@notmail.com>
Newsgroups: alt.comp.lang.php,alt.html.server-side,alt.php,ba.php,comp.lang.php
Subject: Issues with ImageGIF and ImageJPEG functions
Date: Mon, 7 Oct 2002 16:10:33 +0000 (UTC)
Organization: BT Openworld
Lines: 85
Message-ID: <ansblo$88m$1@paris.btinternet.com>
NNTP-Posting-Host: host217-34-234-248.in-addr.btopenworld.com
X-Trace: paris.btinternet.com 1034007033 8470 217.34.234.248 (7 Oct 2002 16:10:33 GMT)
X-Complaints-To: news-complaints@lists.btinternet.com
NNTP-Posting-Date: Mon, 7 Oct 2002 16:10:33 +0000 (UTC)
X-Newsreader: Microsoft Outlook Express 5.50.4807.1700
X-MSMail-Priority: Normal
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700