Title: Remove extension when creating a new image from resizing
Tags: php
Question: i have this code, which i kind of modified a little bit ago.. my problem is that this creates a new thumbnail of the image i upload but it adds a .PNG,.JPEG etc etc extension which i don't need! now, my problem is that if i try to remove from the function the extensions it won't let me create the new image...

What i mean is that if i remove : '.'.$this->ext from the createFile function it won't create the file anymore...

Here is the code... it seems to be a little long but it isn't..

```if (!empty($_FILES)) {


function setFile($src = null) {
    $this-&gt;ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION));

if(is_file($src) &amp;&amp; ($this-&gt;ext == "JPG" OR $this-&gt;ext == "JPEG")) {
        $this-&gt;img_r = ImageCreateFromJPEG($src);
    } elseif(is_file($src) &amp;&amp; $this-&gt;ext == "PNG") {
        $this-&gt;img_r = ImageCreateFromPNG($src);
    } elseif(is_file($src) &amp;&amp; $this-&gt;ext == "GIF") {
        $this-&gt;img_r = ImageCreateFromGIF($src);
    }

$this-&gt;img_w = imagesx($this-&gt;img_r);
    $this-&gt;img_h = imagesy($this-&gt;img_r);
}

function resize($largestSide = 100) {
    $width = imagesx($this-&gt;img_r);
    $height = imagesy($this-&gt;img_r);
    $newWidth = 0;
    $newHeight = 0;

    if ($width &gt; $height) {
        $newWidth = $largestSide;
        $newHeight = $height * ($newWidth / $width);
    } else {
        $newHeight = $largestSide;
        $newWidth = $width * ($newHeight / $height);
    }

    $this-&gt;dst_r = ImageCreateTrueColor($newWidth, $newHeight);
    imagecopyresampled($this-&gt;dst_r, $this-&gt;img_r, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    $this-&gt;img_r = $this-&gt;dst_r;
    $this-&gt;img_h = $newHeight;
    $this-&gt;img_w = $newWidth;
}

function createFile($output_filename = null) {
    if ($this-&gt;ext == "JPG" OR $this-&gt;ext == "JPEG") {
        imageJPEG($this-&gt;dst_r, $this-&gt;uploaddir.$output_filename.'.'.$this-&gt;ext, $this-&gt;quality);
    } elseif($this-&gt;ext == "PNG") {
        imagePNG($this-&gt;dst_r, $this-&gt;uploaddir.$output_filename.'.'.$this-&gt;ext);
    } elseif($this-&gt;ext == "GIF") {
        imageGIF($this-&gt;dst_r, $this-&gt;uploaddir.$output_filename.'.'.$this-&gt;ext);
    }

    $this-&gt;output = $this-&gt;uploaddir.$output_filename.'.'.$this-&gt;ext;
}

function setUploadDir($dirname) {
$this-&gt;uploaddir = $dirname;
}

function flush() {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

imagedestroy($this-&gt;dst_r);
unlink($targetFile);
imagedestroy($this-&gt;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-&gt;setFile($targetFile);
$image-&gt;setUploadDir($targetPath);
$image-&gt;resize(800);
$image-&gt;createFile($_FILES['Filedata']['name']);
$image-&gt;flush();
}
```

Comment: Hey thanks, i'm just modifying it, if i were that capable to build it i wouldn't ask... i mean i understand the logic... but i can't rearrange it the way i need..

Comment: Please don't define functions within an `if` statement. That is a terrible practice, even if it *is* valid PHP.

Comment: BTW you never call any of those functions within the `if` statement; you only define them. That may be why your code isn't working.

Comment: Also, you have an extra close-curly at the end there.


Here is another answer: After formatting your code and removing the extra close-curly at the end (while moving your function definitions outside of your ```if``` statement), your code should look something like this:

```<?php

function setFile($src = null) {
    $this-&gt;ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION));
    if (is_file($src) &amp;&amp; ($this-&gt;ext == "JPG" OR $this-&gt;ext == "JPEG")) {
        $this-&gt;img_r = ImageCreateFromJPEG($src);
    } elseif (is_file($src) &amp;&amp; $this-&gt;ext == "PNG") {
        $this-&gt;img_r = ImageCreateFromPNG($src);
    } elseif (is_file($src) &amp;&amp; $this-&gt;ext == "GIF") {
        $this-&gt;img_r = ImageCreateFromGIF($src);
    }
    $this-&gt;img_w = imagesx($this-&gt;img_r);
    $this-&gt;img_h = imagesy($this-&gt;img_r);
}

function resize($largestSide = 100) {
    $width     = imagesx($this-&gt;img_r);
    $height    = imagesy($this-&gt;img_r);
    $newWidth  = 0;
    $newHeight = 0;

    if ($width &gt; $height) {
        $newWidth  = $largestSide;
        $newHeight = $height * ($newWidth / $width);
    } else {
        $newHeight = $largestSide;
        $newWidth  = $width * ($newHeight / $height);
    }

    $this-&gt;dst_r = ImageCreateTrueColor($newWidth, $newHeight);
    imagecopyresampled($this-&gt;dst_r, $this-&gt;img_r, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    $this-&gt;img_r = $this-&gt;dst_r;
    $this-&gt;img_h = $newHeight;
    $this-&gt;img_w = $newWidth;
}

function createFile($output_filename = null) {
    if ($this-&gt;ext == "JPG" OR $this-&gt;ext == "JPEG") {
        imageJPEG($this-&gt;dst_r, $this-&gt;uploaddir . $output_filename . '.' . $this-&gt;ext, $this-&gt;quality);
    } elseif ($this-&gt;ext == "PNG") {
        imagePNG($this-&gt;dst_r, $this-&gt;uploaddir . $output_filename . '.' . $this-&gt;ext);
    } elseif ($this-&gt;ext == "GIF") {
        imageGIF($this-&gt;dst_r, $this-&gt;uploaddir . $output_filename . '.' . $this-&gt;ext);
    }
    $this-&gt;output = $this-&gt;uploaddir . $output_filename . '.' . $this-&gt;ext;
}

function setUploadDir($dirname) {
    $this-&gt;uploaddir = $dirname;
}

function flush() {
    $tempFile   = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetFile = str_replace('//', '/', $targetPath) . $_FILES['Filedata']['name'];

    imagedestroy($this-&gt;dst_r);
    unlink($targetFile);
    imagedestroy($this-&gt;img_r);
}

if (!empty($_FILES)) { 
    $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-&gt;setFile($targetFile);
    $image-&gt;setUploadDir($targetPath);
    $image-&gt;resize(800);
    $image-&gt;createFile($_FILES['Filedata']['name']);
    $image-&gt;flush();
}
```

This still will not work because you're calling these methods as though they're member functions of an ```Image``` object which isn't defined anywhere. Take a look at Objects and Classes in the PHP manual.

Comment for this answer: Hey thanks for the help, but this is supposed to not add the extension after the files?
Comment for this answer: Hey thanks...well, the code works as it is in fact.. i mean what i needed is just that when the new image is created not to take any extension as it oes for the moment.. but anyways i'm going to take a look again
Comment for this answer: @Al12 This is all your code - I just cleaned it up a bit. Make sure that you're defining an `Image` object as well (you're not doing that here).

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:08/20/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php>', 'Title': 'Remove extension when creating a new image from resizing', 'ViewCount': 103, 'id': 12039604, 'user_AboutMe': '', 'user_AccountId': 1698680.0, 'user_DisplayName': 'Al12', 'user_Reputation': 63.0, 'user_Views': 25.0, 'user_id': 1559406.0}}