...

View Full Version : RESOLVED: Problem with a .jpeg extension where .jpg works OK in a PHP/ Mysql gallery



onoffpaul
07-17-2008, 02:34 PM
Hi,

I am having a problem with uploading files .jpeg extensions to a PHP/Mysql gallery. The .jpg extension work but not the .jpeg extension.

To rectify the problem I tried first to replace the reference to the .jpg extension to .jpeg in my 'functions.php' file to see if that would work. However, the result of that was that the script uploaded the image with the file extension '.jjpeg' (yes, thats a double 'j').

Below is the functions.php file BEFORE I tried the above.

Thanks a mil,

Paul



function uploadImage($inputName, $uploadDir)
{
$image = $_FILES[$inputName];
$imagePath = '';
$thumbnailPath = '';

// if a file is given
if (trim($image['tmp_name']) != '') {
$ext = substr(strrchr($image['name'], "."), 1);

// generate a random new file name to avoid name conflict
// then save the image under the new file name
$imagePath = md5(rand() * time()) . ".$ext";
$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);

if ($result) {
// create thumbnail
$thumbnailPath = md5(rand() * time()) . ".$ext";
$result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);

// create thumbnail failed, delete the image
if (!$result) {
unlink($uploadDir . $imagePath);
$imagePath = $thumbnailPath = '';
} else {
$thumbnailPath = $result;
}
} else {
// the image cannot be uploaded
$imagePath = $thumbnailPath = '';
}

}


return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}

/*
Create a thumbnail of $srcFile and save it to $destFile.
The thumbnail will be $width pixels.
*/
function createThumbnail($srcFile, $destFile, $width, $height, $quality = 75)
{
$thumbnail = '';

if (file_exists($srcFile) && isset($destFile))
{
$size = getimagesize($srcFile);
$w = number_format($width, 0, ',', '');
$h = number_format($height, 0, ',', '');

$thumbnail = copyImage($srcFile, $destFile, $w, $h, $quality);
}

// return the thumbnail file name on sucess or blank on fail
return basename($thumbnail);
}



//Function to create the album thumbnails
function createThumbnail2($srcFile, $destFile, $width, $height, $quality = 75)
{
$thumbnail = '';

if (file_exists($srcFile) && isset($destFile))
{
$size = getimagesize($srcFile);
$w = number_format($width, 0, ',', '');
$h = number_format($height, 0, ',', '');

$thumbnail = copyImage($srcFile, $destFile, $w, $h, $quality);
}

// return the thumbnail file name on sucess or blank on fail
return basename($thumbnail);
}




/*
Copy an image to a destination file. The destination
image size will be $w X $h pixels
*/
function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
{
$tmpSrc = pathinfo(strtolower($srcFile));
$tmpDest = pathinfo(strtolower($destFile));
$size = getimagesize($srcFile);

if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
{
$destFile = substr_replace($destFile, 'jpg', -3);
$dest = imagecreatetruecolor($w, $h);
//imageantialias($dest, TRUE);
} elseif ($tmpDest['extension'] == "png") {
$dest = imagecreatetruecolor($w, $h);
//imageantialias($dest, TRUE);
} else {
return false;
}

switch($size[2])
{
case 1: //GIF
$src = imagecreatefromgif($srcFile);
break;
case 2: //JPEG
$src = imagecreatefromjpeg($srcFile);
break;
case 3: //PNG
$src = imagecreatefrompng($srcFile);
break;
default:
return false;
break;
}

imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);

switch($size[2])
{
case 1:
case 2:
imagejpeg($dest,$destFile, $quality);
break;
case 3:
imagepng($dest,$destFile);
}
return $destFile;

}

ohgod
07-17-2008, 05:41 PM
the reason you were getting jjpeg is because of the substr_replace below. it moves back 3 characters and drops jpg in. so if you take the filename whatever.jpeg, move back 3 characters from the and and drop in jpeg, you'd get whatever.jjpeg


if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
{
$destFile = substr_replace($destFile, 'jpg', -3);

just add another condition for $tmpDest['extension'] == "jpeg" (which actually, just glancing at it i don't see where $tmpDest['extension'] is being set..) and change the starting point in your substr_replace statement.

onoffpaul
07-17-2008, 05:50 PM
Thanks a mil!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum