Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Remove extension when creating new file after resize

    Hi everyone,

    please i'd need yur help on this one, apparently it seems to be easy but i cant get it to work! what i need is that, when i create a new image based on this php script, i don't need the extensions it puts.. i mean, for example... if the image i upload is : test.jpg for the moment the image goes into the folder as test.jpg.JPG while i don't need that...

    How can i modify this script in order to achieve this?
    I tried removing .'.'.$this->ext from the createFile function but then it wont create the file!

    Please take a look at this, it seems a littl bit long code but it isn't... thanks!

    Code:
    if (!empty($_FILES)) {
    
    class Image {
    
    var $uploaddir;
    var $quality = 80;
    var $ext;
    var $dst_r;
    var $img_r;
    var $img_w;
    var $img_h;
    var $output;
    var $data;
    var $datathumb;
    
    function setFile($src = null) {
    $this->ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION));
    if(is_file($src) && ($this->ext == "JPG" OR $this->ext == "JPEG")) {
    $this->img_r = ImageCreateFromJPEG($src);
    } elseif(is_file($src) && $this->ext == "PNG") {
    $this->img_r = ImageCreateFromPNG($src);
    } elseif(is_file($src) && $this->ext == "GIF") {
    $this->img_r = ImageCreateFromGIF($src);
    }
    $this->img_w = imagesx($this->img_r);
    $this->img_h = imagesy($this->img_r);
    }
    
    function resize($largestSide = 100) {
    $width = imagesx($this->img_r);
    $height = imagesy($this->img_r);
    $newWidth = 0;
    $newHeight = 0;
    
    if($width > $height){
    $newWidth = $largestSide;
    $newHeight = $height * ($newWidth / $width);
    }else{
    $newHeight = $largestSide;
    $newWidth = $width * ($newHeight / $height);
    }
    
    $this->dst_r = ImageCreateTrueColor($newWidth, $newHeight);
    imagecopyresampled($this->dst_r, $this->img_r, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    $this->img_r = $this->dst_r;
    $this->img_h = $newHeight;
    $this->img_w = $newWidth;
    }
    
    function createFile($output_filename = null) {
    if($this->ext == "JPG" OR $this->ext == "JPEG") {
    imageJPEG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext, $this->quality);
    } elseif($this->ext == "PNG") {
    imagePNG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
    } elseif($this->ext == "GIF") {
    imageGIF($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
    }
    $this->output = $this->uploaddir.$output_filename.'.'.$this->ext;
    }
    
    function setUploadDir($dirname) {
    $this->uploaddir = $dirname;
    }
    
    function flush() {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
    
    imagedestroy($this->dst_r);
    unlink($targetFile);
    imagedestroy($this->img_r);
    
    }
    
    }
    
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
    
    move_uploaded_file ($tempFile, $targetFile);
    
    $image = new Image();
    $image->setFile($targetFile);
    $image->setUploadDir($targetPath);
    $image->resize(800);
    $image->createFile($_FILES['Filedata']['name']);
    $image->flush();
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You may want to look for a newer script. This was written near a decade ago during the PHP4 days as the class itself predates 5.0.

    If you are on 5.2+ (which in this day I'd expect that you likely are), you can simply change this:
    PHP Code:
    $image->createFile($_FILES['Filedata']['name']); 
    To this:
    PHP Code:
    $sFilename pathino($_FILES['Filedata']['name'], PATHINFO_FILENAME);
    $image->createFile($sFilename); 
    And that should give you the filename without a path or extension.


  •  

    Tags for this Thread

    Posting Permissions

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