Jump to content

Uploading Images


supanoob

Recommended Posts

The Below code is what i am using to upload and resize images. now i have tried uploading 2 images one of them was 300 x 300 and another was 200 x 200 and neither uploaded it cam up with an error saying it was divsion by 0 :S can anyone see why?

[code]$uploaddir = "/public_html/player_images/";
   

    $pext = getFileExtension($imgfile_name);
    $pext = strtolower($pext);
    if (($pext != "jpg")  && ($pext != "jpeg"))
    {
        print "<h1>ERROR</h1>Image Extension Unknown.<br>";
        print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
        print "The file you uploaded had the following extension: $pext</p>\n";

     
        unlink($imgfile);
        exit();
    }




 
    $imgsize = GetImageSize($imgfile);


    if (($imgsize[0] > 200) || ($imgsize[1] > 200))
    {

        $tmpimg = tempnam("/tmp", "MKUP");


            1. decompress jpeg image to pnm file (a raw image type)
            2. scale pnm image
            3. compress pnm file to jpeg image

       
 
        system("djpeg $imgfile >$tmpimg");
       


        system("pnmscale -xy 200 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");

 
        unlink($tmpimg);

    }


    $final_filename = str_replace(" ", "_", $imgfile_name);
    $newfile = $uploaddir/$final_filename;
   
 
    if (is_uploaded_file($imgfile))
    {

      if (!copy($imgfile,"$newfile"))
      {

          print "Error Uploading File.";
          exit();
      }
    }


    unlink($imgfile);

   
    echo "<img src=\"/public_html/player_images/$final_filename\">";
[/code]
Link to comment
Share on other sites

heres an example how to resize an image ok.

gd must be enabled in the php.ini


[code]

<?php

// Load image
$image = open_image('flower.jpg');
if ($image === false) { die ('Unable to open image'); }

// Get original width and height
$width = imagesx($image);
$height = imagesy($image);

// New width and height
$new_width = 150;
$new_height = 100;

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Display resized image
header('Content-type: image/jpeg');
imagejpeg($image_resized);
die();

?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.