View Full Version : thumbnail func help

07-27-2012, 04:46 PM

hope some one can help me fix, here is how i call it:

create_thumb($aConfig, $files, $new_name, $thumbDir .'/'. $album_id .'/');

and here's the func:

function create_thumb(array $aConfig, $image_file, $name, $thumbDir) {
$iDimension = getimagesize($image_file);
$type = $iDimension[2];
echo 'Type: '. $type;

if ($type == "image/jpeg") { $tempThumb = imagecreatefromjpeg($image_file); }
elseif ($type == "image/bmp") { $tempThumb = imagecreatefromwbmp($image_file); }
elseif($type == "image/png") { $tempThumb = imagecreatefrompng($image_file); }
elseif ($type == "image/gif") { $tempThumb = imagecreatefromgif($image_file); }

$width = $iDimension[0]; // uploaded image width
$height = $iDimension[1]; // uploaded image height

$ratio = $width / $height; // calculate the ratio

if ($ratio > 1 ) {
$newW = $aConfig['custom_pixels'];
$newH = $aConfig['custom_pixels'] / $ratio;
} else {
$newH = $aConfig['custom_pixels'];
$newW = $aConfig['custom_pixels'] * $ratio;

$thumb = imagecreatetruecolor($newW, $newH);

//the resizing is going on here!
imagecopyresampled($thumb, $tempThumb, 0, 0, 0, 0, $newW, $newH, $width, $height);

//finally, save the image
imagejpeg($thumb, $thumbDir .'/thumb_' . $name);

// clean up

I don't see the problem, any help............

07-27-2012, 04:54 PM
What's the issue you're having? The code looks OK, not withstanding I donno why you're multiplying or dividing by the ratio on only the height...

07-27-2012, 05:02 PM
it's just not uploading.

07-27-2012, 06:50 PM
You've tested out each step, echo'ed to make sure variables are what you expect, etc? And that the directory is writable?

07-27-2012, 07:44 PM
create_thumb($aConfig, $files, $new_name, $thumbDir .'/'. $album_id .'/');

function create_thumb(array $aConfig, $image_file, $name, $thumbDir)
What is the word array doing in that second line? If $aConfig is an array, php will know it already.

07-27-2012, 09:16 PM
array is added to a function signature to ensure its datatype. It guarantees that the item provided is an array. You do the same thing with objects, and if you want you can override the error handling to provide datatype strength to all custom PHP functions with whatever datatypes you want.

What is this: imagecopyresampled($thumb0, $tempThumb, 0, 0, 0, 0, $newW, $newH, $width, $height); ? There is no $thumb0 declared anywhere. That has to be a valid resource created by the imagegd.

07-27-2012, 11:18 PM
This is what iis in my config;
$config['custom_pixels'] = $_POST['max_dimensions'];

As you see my func above, what could the problem be?

all typos are fixed fyi.

07-28-2012, 12:58 AM
Ok i've
echo 'thumbnail: '. $aConfig['custom_pixels']; get nothing, then i did:

// Create the thumbnail
if(isset($aConfig['custom_pixels']) && !empty($aConfig['custom_pixels']) && is_numeric($aConfig['custom_pixels'])) {
create_thumb($aConfig, $url, $new_name, $thumb);
} else {
trigger_error('<div align="center">Cannot create your thumbnail; ensure GD extension is loaded.</div>', E_USER_WARNING);
} and im getting the error..............

07-28-2012, 01:19 AM
Do you have an input in your form named max_dimensions ?

07-28-2012, 01:51 AM
Yea ive checked that, when i echo $aConfig['custom_pixels'] notin's there

07-28-2012, 02:44 AM
I don't see where you convert $config['custom_pixels']
to $aConfig['custom_pixels'] ?

Sometimes it's easier to write you own code from the top down than to modify someone else's, because you know what everything does.

07-28-2012, 03:30 PM
$aConfig is passed to the func... So then I c an use $aConfig['custom_pixels']??

07-28-2012, 03:38 PM
Do a print_r of $aConfig and see what you get, you are definitely missing some data somewhere. When you work with any kind of array, print_r is you friend and can find problems you didn't know you would have.

07-28-2012, 04:02 PM
Ok done it mate, it seem its not picking up: . [custom_pixels] =>

here's my input:
<td><h2>Desired Width:</h2></td>
<td><select name="max_dimensions">
<option value="50">None</option>
<option value="90">90px x 90px</option>
<option value="200">200px x 200px</option>
<option value="300">300px x 300px</option>
<option value="400">400px x 400px</option>
<option value="500">500px x 500px</option>
</select><font color="Red" size="-1"> - Optional</font></td></tr>


07-28-2012, 05:22 PM
You can define or re-define any array element at any time in the process.
Put this line below where you have defined:


$aConfig['custom_pixels'] = $config['custom_pixels'];

That should fix it.

07-28-2012, 07:09 PM
kk don that, still a problem so i defined it to a normal var like:
$custom_px = $_POST['maxdimensions']; that works, so it dont like:
$config['custom_pixels'] = $_POST['max_dimensions']; not really a biggie as it doesn't an shouldn't be changed.

so im wandering can i make it constant?