www.webdeveloper.com
Results 1 to 6 of 6

Thread: creating images using the GD thing

  1. #1
    Join Date
    Aug 2004
    Posts
    192

    creating images using the GD thing

    I am trying to build an image upload script, but am running into a few issues.
    Once the images are uploaded, im trying to determine the type by looking at the format, and to simply copy the image using all those if statements things, is this right?
    PHP Code:
    $ext pathinfo($imgPATHINFO_EXTENSION);        
    if (
    $ext == "png") {
        
    $new imagecreatefrompng($img);
    } elseif  (
    $ext ==  "gif") {
        
    $new imagecreatefromgif($img);
    } elseif (
    $ext == "jpg") {
        
    $new imagecreatefromjpeg($img);
    } elseif (
    $ext == "bmp") {
        
    $new imagecreatefromwbmp($img); 
    Thanks
    I'm a deer hunter. I go all the time with my dad. One thing about deer, they have very good vision. One thing about me...I am better at hiding than they are at vision.

  2. #2
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    you can put the image types in an array. Here is an example

    http://www.make-mywebsite.com/how-to...orm-using-php/

    Code:
    <?php
     
    extract($_POST);
     
    $name;
     
    $email;
     
    $fileType = $_FILES['prodImg']['type'];
     $fileSize = $_FILES['prodImg']['size'];
     
    if($fileSize/1024 > '2048') {
     echo 'Filesize is not correct it should equal to 2 MB or less than 2 MB.';
     exit();
     } //FileSize Checking
     
    if($fileType != 'image/png' &&
     $fileType != 'image/gif' &&
     $fileType != 'image/jpg' &&
     $fileType != 'image/jpeg' &&
     $fileType != 'application/vnd.openxmlformats-officedocument.wordprocessingml.document ' &&
     $fileType != 'application/zip' &&
     $fileType != 'application/pdf'
     )     {
     echo 'Sorry this file type is not supported we accept only. Jpeg, Gif, PNG, or ';
     exit();
     } //file type checking ends here.
     $upFile = 'uploads/'.date('Y_m_d_H_i_s').$_FILES['prodImg']['name'];
     
    if(is_uploaded_file($_FILES['prodImg']['tmp_name'])) {
     if(!move_uploaded_file($_FILES['prodImg']['tmp_name'], $upFile)) {
     echo 'Problem could not move file to destination. Please check again later. <a href="index.php">Please go back.</a>';
     exit;
     }
     } else {
     echo 'Problem: Possible file upload attack. Filename: ';
     echo $_FILES['prodImg']['name'];
     exit;
     }
     $prodImg = $upFile;
     } //File upload ends here.
     
    $upFile;
     ?>

  3. #3
    Join Date
    Mar 2011
    Posts
    1,139
    I would avoid relying on the file extension because you can't really count on there being one all the time since neither Macs nor Windows systems require file name extensions. It's very unlikely that you would run into a graphics file without an extension, but it's always best to use a method that returns the MIME-type such as the one used in the example code that Nick posted:

    $_FILES['imageFieldName']['type']

    and check for values like "image/png", etc. to determine the appropriate imagecreatefrom() function as you're doing now. It's just one less thing to worry about if you ever run into problems. Good luck!
    Rick Trethewey
    Rainbo Design

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    Use the getimagesize() function, and check the returned array's index 2 or key 'mime' to get the image type. (It will return false if it can not identify the file as a known image type.)

    Here's a simple example where I wanted to verify a file was a JPEG image:
    PHP Code:
       $attrs = @getimagesize($imgFile);
       if(
    $attrs == false or $attrs[2] != IMG_JPEG)
       {
          
    $error "Uploaded image is not JPEG or is not readable by this page.";
          return 
    false;
       } 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    I am curious nogdog why do you suggest limiting your self to a JPEG instead of an array allowing multiple image types?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    Quote Originally Posted by Nicholas Diaz View Post
    I am curious nogdog why do you suggest limiting your self to a JPEG instead of an array allowing multiple image types?
    Because the application I wrote that for only accepted JPEGs (for whatever reason), and I didn't feel like editing the code here (i.e.: was too lazy) to make it more inclusive. I figured I'd just point y'all in the general direction and let you decide what you want to accept/reject. You could just check for false if all you want to do is verify it's some sort of image that GD recognizes, or you could use in_array() with a list of possible types to allow.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles