hey i have a code to resize images on upload.
it works great for all .gif files.
but it just dont want to work for .jpg or .jpeg images, could you maybe see what is wrong?
PHP Code:
if($filename['name'] != null) {
if(@file_exists("images/avatars/".$user_name."".$ext)) {
$delete = @unlink("images/avatars/".$user_name."".$ext);
}
$dimensions = getimagesize($filename['tmp_name']);
$aspectRat = (float)($dimensions[1] / $dimensions[0]);
$new_max_width = 80;
$new_max_height = 80;
if(($dimensions[0] <= $new_max_width) && ($dimensions[1] <= $new_max_width)) {
copy($filename['tmp_name'], "images/avatars/".$user_name."".$ext);
$newX = $dimensions[0];
$newY = $dimensions[1];
}
else {
$width_ratio = $dimensions[0] / $new_max_width;
$height_ratio = $dimensions[1] / $new_max_height;
if($width_ratio >= $height_ratio) {
$newY = $new_max_width * $aspectRat;
$newX = $new_max_width;
}
else {
$newY = $new_max_height;
$newX = $new_max_height * (1/$aspectRat);
}
$destImg = ImageCreateTrueColor($newX, $newY);
echo $dimensions[2];
if ($dimensions[2] == 1) {
$sourceImg = ImageCreateFromGIF($filename['tmp_name']);
}
elseif ($dimensions[2] == 2) {
$sourceImg = ImageCreateFromJPEG($filename['tmp_name']);
}
else
{
echo "This file cannot be used. It is not a jpg, gif<br>";
}
imagecopyresampled($destImg, $sourceImg, 0, 0, 0, 0, $newX, $newY, $dimensions[0], $dimensions[1]);
imagejpeg($destImg, "images/avatars/".$user_name."".$ext);
echo "<img src='images/avatars/".$user_name."".$ext."'>";
}
}
it gives me the message This file cannot be used. It is not a jpg, gif (in the last else function you can see) even if its a .jpg or .jpeg (dont really know the difference).
greets
|