Click to See Complete Forum and Search --> : How do I Specify Image Size With a PHP Script?


qasabah
12-11-2006, 09:29 PM
I have a php script that calls an image and various other items but I decided I wanted to specify the image size rather than have it look for it and apply it itself.

Problem is I don't know anything about php so my effort probably resembles what a monkey flailing around with a typewriter might produce.

Any help would be greatly appreciated.

function showImage( $ini=null ) {
global $IMG_CONFIG_FILE;
# if no custom ini file has been specified, use the default
$ini_file = $ini ? $ini : $IMG_CONFIG_FILE;
# read the config file into an array or die trying
$images = @parse_ini_file($ini_file,true);
if (! $images) {
die('Unable to read ini file.');
}
# pick a random image from the parsed config file
$img = array_rand($images);
# get the selected image's css id if one exists
$id = $images[$img]['id'] ?
sprintf( ' id="%s" ', $images[$img]['id'] ) :
'';
# get the selected image's css class if one exists
$class = $images[$img]['class'] ?
sprintf( ' class="%s" ', $images[$img]['class'] ) :
'';
# get selected image's dimensions
$width = $images[$img]['width'] ?
sprintf( ' width="%s" ',$images[$img]['width'] ) :
";

# get selected image's dimensions
$height = $images[$img]['height'] ?
sprintf( ' height="%s" ',$images[$img]['height'] ) :
";
# if an url was specified, output the opening A HREF tag
if ( $images[$img]['url'] ) {
printf(
'<a href="%s" title="%s">',
$images[$img]['url'],
$images[$img]['title']
);
}


# output the IMG tag
printf(
'<img src="%s" alt="%s" width="%s" height="%s" %s %s%s/>',
$images[$img]['src'],
$images[$img]['alt'],
$id,
$class
$images[$img]['width'],
$images[$img]['height'],
);
# if an url was specified, output the closing A HREF tag
if ( $images[$img]['url'] ) {
echo('</a>');
}



}

?>

[plus44]
src = music/music_images/plus44.jpg
alt = Plus 44- When your Heart Stops Beating
url = music/heartstops.html
title = Plus 44- When Your Heart Stops Beating
width = 150
height = 136

ShrineDesigns
12-11-2006, 09:52 PM
for the <img> element? you don't need to specify height or width, the only required attributes are src and alt

qasabah
12-11-2006, 10:02 PM
I know they're not required. I had an automatic size fetching snippet in there which I removed because I want to specify the displayed image size.

I could just duplicate the image files, resize them and put them in a separate folder but I'd rather specify the image size with a script and save time and server space.

qasabah
12-12-2006, 01:02 PM
I've figured out how to specify a height and width for a randomly called image using php.

For posterity here is my method...

# file containg image descriptions

$IMG_CONFIG_FILE = 'rimages.ini';



# Body of code

function showImage( $ini=null ) {
global $IMG_CONFIG_FILE;
# if no custom ini file has been specified, use the default
$ini_file = $ini ? $ini : $IMG_CONFIG_FILE;
# read the config file into an array or die trying
$images = @parse_ini_file($ini_file,true);
if (! $images) {
die('Unable to read ini file.');
}
# pick a random image from the parsed config file
$img = array_rand($images);
# get the selected image's css id if one exists
$id = $images[$img]['id'] ?
sprintf( ' id="%s" ', $images[$img]['id'] ) :
'';
# get the selected image's css class if one exists
$class = $images[$img]['class'] ?
sprintf( ' class="%s" ', $images[$img]['class'] ) :
'';

# if an url was specified, output the opening A HREF tag
if ( $images[$img]['url'] ) {
printf(
'<a href="%s" title="%s">',
$images[$img]['url'],
$images[$img]['title']
);
}


# output the IMG tag
printf(
'<img src="%s" alt="%s" width="%s" height="%s" %s%s/>',
$images[$img]['src'],
$images[$img]['alt'],
$images[$img]['width'],
$images[$img]['height'],
$id,
$class

);
# if an url was specified, output the closing A HREF tag
if ( $images[$img]['url'] ) {
echo('</a>');
}



}

?>

Notice I've removed the width and height statements from the script except for in these two locations.

The .ini file doesn't require any modification from that shown in the original post of the thread.

Like I said I'm like a monkey with a typewriter. The advantage of being said monkey is that eventually by chance alone you'll get it right.