Re: image resize problems

From: Date: Wed, 02 Jun 2004 17:03:27 +0000
Subject: Re: image resize problems
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-187463@lists.php.net to get a copy of this message
Edward Peloke wrote:
I have looked at several of the functions and tried a few tutorials online but am not having much luck for some reason resizing images. I have this code which works fine but everytime I try to add some resizing code, I get the error that the image is not a valid image resource. I have even hardcoded the values to an image I know exists and get the same error. I would like to resize the images on upload to 250 wide but nothing seems to work. $upload_dir = "../images/property_$id"; //When REGISTERED_GLOBALS are off in php.ini
  $_POST    = $HTTP_POST_VARS;
  $_GET     = $HTTP_GET_VARS;
//Upload the file
    if($_GET['method'] == "upload")
    {
      $file_array = $HTTP_POST_FILES['file'];
      $_SESSION['message'] = "";
      $uploads = false;
      for($i = 0 ; $i < $files_to_upload; $i++)
      {
        if($_FILES['file']['name'][$i])
        {
          $uploads = true;
          if($_FILES['file']['name'][$i])
          {
            $file_to_upload = $upload_dir."/".$_FILES['file']['name'][$i];
            $thisName=$_FILES['file']['name'][$i];
                        $sql->query("update properties set
main_thumb='$thisName' where id=$id and main_thumb is null"); move_uploaded_file($_FILES['file']['tmp_name'][$i],$file_to_upload);
            chmod($file_to_upload,0777);
            //  add image resize
              $_SESSION['message'] .= $_FILES['file']['name'][$i]."
uploaded.<br>";
          }
        }
      }
      if(!$uploads)  $_SESSION['message'] = "No files selected!";
    }
Thanks, Eddie WARNING: The information contained in this message and any attachments is intended only for the use of the individual or entity to which it is addressed. This message may contain information that is privileged, confidential and exempt from disclosure under applicable law. It may also contain trade secrets and other proprietary information for which you and your employer may be held liable for disclosing. You are hereby notified that any unauthorized dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify HIPAA@echoman.com by E-Mail and then destroy this communication in a manner appropriate for privileged information.
Here's some code I use: $fileinfo = pathinfo($file); switch(strtolower($fileinfo['extension'])) { case 'png':
    $ifh = imagecreatefrompng($dir.$file);
    break;
case 'jpg': case 'jpeg': case 'jpe':
    $ifh = imagecreatefromjpeg($dir.$file);
    break;
case 'gif':
    $ifh = imagecreatefromgif($dir.$file);
    break;
default:
    echo '  I don\'t know what to do with the extension '.$fileinfo['extension']."\n";
    exit;
    break;
} $swidth = imagesx($ifh); $sheight = imagesy($ifh); $ih = imagecreatetruecolor($width + $edge * 2, $height + $edge * 2); imagealphablending($ih, true); if($swidth > $sheight) {
    $nwidth = $width;
    $nheight = (int)($sheight / $swidth * $nwidth);
    $x = 0;
    $y = ($height - $nheight) / 2;
} else if($sheight < $swidth) {
    $nheight = $height;
    $nwidth = (int)($swidth / $sheight * $nheight);
    $y = 0;
    $x = ($width - $nwidth) / 2;
} else {
    $nwidth = $width;
    $nheight = $height;
    $x = $y = 0;
} imagecopyresampled($ih, $ifh, $x + $edge, $y + $edge, 0, 0, $nwidth, $nheight, $swidth, $sheight); header('Content-Type: image/jpeg'); imagejpeg($ih); -- paperCrane <Justin Patrin>

« previous php.general (#187463) next »