Page 1 of 1

Image files with .JPG extension not displaying

Posted: Thu Mar 18, 2010 9:13 am
by justinbeeler
I have built a social networking site that allows members to upload their photos. I'm running Apache on Linux through Fatcow. I created a script that displays the thumbnail images and does just fine for .jpg, .jpeg, .png and .gif. But if someone uploads a file that has the .JPG extension, it doesn't display. I read somewhere that Apache treats .JPG differently from .jpg. So I changed the extensions on all of the files and changed them in the database too and that didn't fix it. So there must be something embedded in .JPG that apache recognizes and doesn't care about the extension being .jpg even if you change it. Does this make sense?

Here is the script that loads the image, resizes and crops it:

Code: Select all

 
<?php
 
include("config.php");
$fileName = $_GET['file'];
$maxSize = $_GET['size'];
$URL = $basePath . "/images/players/" . $fileName;
$info = getimagesize($URL);
$mime = image_type_to_mime_type($info[2]);
header("Content-type: $mime");
// create an image resource
$t_w = 100;
$t_h = 100;
switch($info[2])
{
    case IMAGETYPE_GIF:
    $src_image = imagecreatefromgif($URL);
    break;
    case IMAGETYPE_JPEG:
    $src_image = imagecreatefromjpeg($URL);
    break;
    case IMAGETYPE_PNG:
    $src_image = imagecreatefrompng($URL);
    break;
}
/*
$src_image = imagecreatefromjpeg($URL);
if(!$src_image){
    $src_image = imagecreatefromjpeg("images/not_available.jpg");
}*/
 
 
// store the width and height values of the image
$orig_h = imagesy($src_image);
$orig_w = imagesx($src_image);
 
// test dimensions of image resource and set thumbnail width height to maintain correct aspect ratio
if($orig_w > $orig_h){
    $thumb_w = $maxSize;
    $thumb_h = $orig_h * ($maxSize / $orig_w);
}
if($orig_w < $orig_h){
    $thumb_h = $maxSize;
    $thumb_w = $orig_w * ($maxSize / $orig_h);
}
if($orig_w == $orig_h){
    $thumb_w = $maxSize;
    $thumb_h = $maxSize;
}
$x_mid = $thumb_w/2;
$y_mid = $thumb_h/2;
 
$dst_image = imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($dst_image,$src_image,0,0,($x_mid-($t_w/2)),($y_mid-($t_h/2)),$thumb_w,$thumb_h,$orig_w,$orig_h);
 
switch($info[2])
{
    case IMAGETYPE_GIF:
    imagegif($dst_image);
    break;
    case IMAGETYPE_JPEG:
    imagejpeg($dst_image);
    break;
    case IMAGETYPE_PNG:
    imagepng($dst_image);
    break;
}
 
imagedestroy($dst_image);
imagedestroy($src_img);
 
 
?>
 
 
The way I use it on the page is like this:

Code: Select all

 
<img src="image_crop.php?file=filename.jpg&size=150" />
 

Can someone please help?