Manntis 08-11-2005, 02:14 PM I'm trying to pull images from a myriad of folders, based on filenames stored in a MySQL database, and display said images in a dynamically created catalogue .php page.
About half of the images are .jpgs and half are .gifs. Because the filenames are created from the item sku, there's no actual extension for the display script. Rught now I'm calling <img src="folder/$sku.jpg"> but this, of course, only displays .jpgs with .gifs coming up as broken images.
How can I have it read the filename based on $sku and determine the actual extansion? I've found GetName(), but that only seems to work when uploading a file, nut reading already-existing filenames on the server.
help?
Manntis 08-11-2005, 05:14 PM n/m
Cosby 08-11-2005, 07:14 PM should have pmed me :) sounds like you could have just done
if(preg_match("/.jpg/",$sku))
{
$ext = '.jpg';
}
else
{
$ext ='.gif';
}
echo "<img src=\"folder/$sku.$ext\">";
if not that you can get into the image functions ... imagecreatefromjpeg or whatever... pain in the ass but I have a few examples I could pull up from where I wrote the watermark scripts all over this site.
Manntis 08-12-2005, 12:24 AM I ended up using
$imagepath='folder/'.$category.'/'.$sku;
if (file_exists($imagepath.".jpg"))
{ $pictype = '.jpg'; }
else
{ $pictype = '.gif'; }
print ("<img src='{$imagepath}$pictype' alt='$name' vspace=1>");
theloudroom 08-12-2005, 08:11 AM Using the UNIX "file" command will actually look at the contents of the file to determine the type.
Manntis 08-12-2005, 11:34 AM Had to use php to make the script transferrable to non-unix servers.
Cosby 08-12-2005, 05:32 PM you'll want to change your code to preg_match ... it is a lot faster .. you could even use strstr which is even faster
Manntis 08-12-2005, 05:37 PM My file structure is fairly efficient so I don't notice any difference between the speed when they were all .jpg and now that the check code is added to allow 'gifs too.
edit: just tried preg_match and I can't figure out how to insert $imagepath in it so the code will know what folder, specifically, I'm drawing the $sku image from
Cosby 08-13-2005, 03:26 AM Actually, I misread your first post. Try this if your version of php supports it. Let me know if it doesn't:
<?php
$imagepath='folder/'.$category.'/'.$sku;
$extension = image_type_to_extension(exif_imagetype($imagepath) ,true);
$image = $imagepath.$extension;
print "<img src=\"$image\" alt=\"$name\" vspace=1>\n";
?>
Cosby 08-13-2005, 03:29 AM if exif doesn't work you can use getimagesize() and if image_type_to_extension() isn't supported you can use this:
<?
if(!function_exists('image_type_to_extension'))
{
function image_type_to_extension($imagetype,$include_dot=fa lse)
{
if(empty($imagetype)) return false;
$dot = $include_dot ? $dot.'.' : '';
switch($imagetype)
{
case IMAGETYPE_GIF : return $dot.'gif';
case IMAGETYPE_JPEG : return $dot.'jpg';
case IMAGETYPE_PNG : return $dot.'png';
case IMAGETYPE_SWF : return $dot.'swf';
case IMAGETYPE_PSD : return $dot.'psd';
case IMAGETYPE_WBMP : return $dot.'wbmp';
case IMAGETYPE_XBM : return $dot.'xbm';
case IMAGETYPE_TIFF_II : return $dot.'tiff';
case IMAGETYPE_TIFF_MM : return $dot.'tiff';
case IMAGETYPE_IFF : return $dot.'aiff';
case IMAGETYPE_JB2 : return $dot.'jb2';
case IMAGETYPE_JPC : return $dot.'jpc';
case IMAGETYPE_JP2 : return $dot.'jp2';
case IMAGETYPE_JPX : return $dot.'jpf';
case IMAGETYPE_SWC : return $dot.'swc';
default : return false;
}
}
}
?>
the way I wrote is a lot cleaner, less buggy and supports png,gif,jpg,bmp,etc.
Manntis 08-13-2005, 12:37 PM thanks :) but for whatever reason that code isn't working in my application.
The one I devised (with help from zyounker) is working, and fairly rapidly.
Cosby 08-13-2005, 02:06 PM You can use whatever code you like but I assure you that is a bad idea. Send me the link where you're running it and I'll show you why through pm. The following code is way more versatile and secure and it is running on illstreet at http://www.illstreet.com/test.php
it shows the file 'www.illstreet.com/images/00FDFOC2DEV-010C'
one more thing. When you write echos and prints use ' instead of " because php won't parse the '. Also keep in mind that echo is a little faster than print so you should avoid it unless you need it for an expression. This has marginal effects in the larger picture.
do what ya want ;)
<?php
if(!function_exists('image_type_to_extension'))
{
function image_type_to_extension($imagetype,$include_dot=fa lse)
{
if(empty($imagetype)) return false;
$dot = $include_dot ? $dot.'.' : '';
switch($imagetype)
{
case IMAGETYPE_GIF : return $dot.'gif';
case IMAGETYPE_JPEG : return $dot.'jpg';
case IMAGETYPE_PNG : return $dot.'png';
case IMAGETYPE_SWF : return $dot.'swf';
case IMAGETYPE_PSD : return $dot.'psd';
case IMAGETYPE_WBMP : return $dot.'wbmp';
case IMAGETYPE_XBM : return $dot.'xbm';
case IMAGETYPE_TIFF_II : return $dot.'tiff';
case IMAGETYPE_TIFF_MM : return $dot.'tiff';
case IMAGETYPE_IFF : return $dot.'aiff';
case IMAGETYPE_JB2 : return $dot.'jb2';
case IMAGETYPE_JPC : return $dot.'jpc';
case IMAGETYPE_JP2 : return $dot.'jp2';
case IMAGETYPE_JPX : return $dot.'jpf';
case IMAGETYPE_SWC : return $dot.'swc';
default : return false;
}
}
}
$imagepath = 'images/00FDFOC2DEV-010C';
//$imagepath='folder/'.$category.'/'.$sku;
if(!$extension = image_type_to_extension(exif_imagetype($imagepath) ,true))
{
echo 'error, file doesn\'t exist';
exit();
}
else
{
$image = $imagepath.$extension;
print "<img src=\"$image\" alt=\"$name\" vspace=1>\n";
}
?>
Manntis 08-13-2005, 02:54 PM unsing it on the main page at http://www.piratesclothing.com/
as I've said, I've tried your coding and my server doesn't interpret it properly.
Cosby 08-13-2005, 09:00 PM What's the error you get? That code should be fine for any version of php that any sensible person would run. Tested it on xp and linux.
Manntis 08-13-2005, 11:51 PM no error - just instead of the images and associated text showing up it blanks out that part of the page. The associated HTML that should have been dynamically generated is missing when the server sends the info to the viewers browser.
Why is if (file_exists($imagepath.".jpg")) a bad idea? It runs quickly and seems to be error free...
|