Click to See Complete Forum and Search --> : image resizing


confused again
03-21-2005, 06:56 PM
Well, this is hard to explain but i've tried for hours to get this simple thing to work.

I'm trying to resize images, this is the code i've come up with:


$imagesize = getimagesize("$files[$i]['name']");
$origwidth=$imagesize[0];
$origheight=$imagesize[1];
$maxwidth="100";

if ($origwidth>$maxwidth) {
$percent=$maxwidth/$origwidth;
$new_height=$origheight*$percent;
$new_width=$maxwidth;

} else {
$new_height=$origheight;
$new_width=$origwidth;
}



Now here is the code that prints each image out thats in the directory.


for ($i = 1; $i <= $filecounter; $i++) {


$allowed_prefix = $userid_log . '_';
$len = strlen($allowed_prefix);
if (substr($files[$i]['name'], 0, $len) == $allowed_prefix) {

$fileoutput.='<table border="1" cellpadding="10" cellspacing="0" width="95%" bordercolor="#000000" bgcolor="#A4B2C8">';

$fileoutput.='<tr>';

$fileoutput.=' <td width="30"><input type="checkbox" name="file[]" value="'.$files[$i]['name'].'"></td>';

$fileoutput.=' <td width="100"><img src="../../pics/'.$files[$i]['name'].'" width="'.$new_width.'" height="'.$new_height.'"></td>';


$fileoutput.=' <td>'.$files[$i]['name'].'</td>';

$fileoutput.=' <td width="100">'.$files[$i]['size'].'</td>';

$fileoutput.='</font></td></tr>';
$fileoutput.='</table><p>';

}
}


And here is the code that determines what $files[$i]['name'] is:



if ($dh = opendir($dir)) {
$dircount=0;
$filecount=0;
while (($file = readdir($dh)) !== false) {
if(filetype($dir.$file)=="dir"){
$dircount++;
if($dircount > 2){

$dir_names[$dircount-2] = $file;
}
} else {
$filecount++;
$files[$filecount]['name'] = $file;
}



The problem is, the image script isn't determining the dimentions for each individual image, and i'm not sure how to fix this..

ShrineDesigns
03-21-2005, 09:02 PM
aspect ratio: ratio = width / height
is wide: ratio > 1
is tall: ratio < 1
is square: ratio == 1
example<?php
$max = 200; // maximum resized size

$src_path = 'path/to/old-image.jpg';
$dst_path = 'path/to/new-image.jpg';

list($src_w, $src_h) = getimagesize($src_path);

$ratio = $src_w / #src_h;

$dst_w = $max;
$dst_h = $max;

if($ratio > 1)
{
$dst_w *= $ratio;
}
else if($ratio < 1)
{
$dst_h /= $ratio;
}
$src_im = imagecreatefromjpeg($src_path);
$dst_im = imagecreatetruecolor($dst_w, $dst_h);
imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
imagejpeg($dst_path);
?>

confused again
03-21-2005, 09:32 PM
I don't think the resize script is the problem. I think this is the problem:

$fileoutput.=' <td width="100"><img src="../../pics/'.$files[$i]['name'].'" width="'.$new_width.'" height="'.$new_height.'"></td>';


and


if ($origwidth>$maxwidth) {
$percent=$maxwidth/$origwidth;
$new_height=$origheight*$percent;
$new_width=$maxwidth;

} else {
$new_height=$origheight;
$new_width=$origwidth;
}


Here the new dimensions are assigned for "$files[$i]['name']", which is the picture file, but it seems like in the img tag the $new_width and $new_height variables don't know which dimensions go to which picture.

ShrineDesigns
03-22-2005, 01:23 AM
hmm...

put this part inside the loop and it should workif ($origwidth>$maxwidth) {
$percent=$maxwidth/$origwidth;
$new_height=$origheight*$percent;
$new_width=$maxwidth;

} else {
$new_height=$origheight;
$new_width=$origwidth;
}

confused again
03-22-2005, 04:40 PM
yeah, i figured that would work, but it didn't. I also tried putting the whole thing in the loop even including the

$imagesize = getimagesize("$files[$i]['name']");
$origwidth=$imagesize[0];
$origheight=$imagesize[1];
$maxwidth="100";

but that didn't work either.