0

Possible Duplicate:
How to convert all images to JPG format in PHP?

I'm trying to pass the username into the filename and have the file end with .jpg So the output would be "user-mycoolnewimage.jpg". Any suggestions on how to do this as currently is changes to "randnumber-mycoolnewimage.png" which isn't what I want. The random number doesn't help server logs and the image needs to end with jpg since it's being converted.

$file = rand(0, 10000000).$_FILES['t1']['name'];
if (move_uploaded_file($_FILES['t1']['tmp_name'], $file)) {
    if($fp = fopen($file,"rb", 0)) {
        $picture = fread($fp,filesize($file));
        fclose($fp);
        $image = imagecreatefrompng($file);
        imagejpeg($image, $file, 70);
        imagedestroy($image);
        $tag1 = '<img src="'.$file.'" alt="" class="default" />';
        //unlink($file);        
     }
}
Community
  • 1
  • 1
Blynn
  • 1,411
  • 7
  • 27
  • 48

1 Answers1

1

I think what you're looking for is basename (documented here)

One thing you could do is rework a bit your script to have something where you create two different $file variables, one holding the initial png and the other one holding the jpeg path. You will also need to unlink your png once done:

$source = $username . "-" . $_FILES['t1']['name'];
$destination = $username . "-" . basename($_FILES['t1']['name']) . ".jpg";
if (move_uploaded_file($_FILES['t1']['tmp_name'], $source)) {
    if($fp = fopen($source,"rb", 0)) {
        $picture = fread($fp,filesize($source));
        fclose($fp);
        $image = imagecreatefrompng($source);
        imagejpeg($image, $destination, 70);
        imagedestroy($image);
        $tag1 = '<img src="'.$file.'" alt="" class="default" />';
        unlink($source);        
     }
}
emartel
  • 7,712
  • 1
  • 30
  • 58