tail Posted January 14, 2010 Share Posted January 14, 2010 I'm trying to resize an image, maintaining its aspect ratio, and then add a drop shadow to it. However, the script simply outputs a 5x5 black image. Here is the code: function thumbnail($inputFileName, $maxsize) { $info = getimagesize($inputFileName); $type = isset($info['type']) ? $info['type'] : $info[2]; $width = isset($info['width']) ? $info['width'] : $info[0]; $height = isset($info['height']) ? $info['height'] : $info[1]; // Calculate aspect ratio $wRatio = $maxSize / $width; $hRatio = $maxSize / $height; // Using imagecreatefromstring will automatically detect the file type $sourceImage = imagecreatefromstring(file_get_contents($inputFileName)); // Calculate a proportional width and height no larger than the max size. if ( ($width <= $maxSize) && ($height <= $maxSize) ) { $tHeight=$height; $tWidth=$width; } elseif ( ($wRatio * $height) < $maxSize ) { $tHeight = ceil($wRatio * $height); $tWidth = $maxSize; } else { $tWidth = ceil($hRatio * $width); $tHeight = $maxSize; } $thumb = imagecreatetruecolor($tWidth, $tHeight); imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height); imagedestroy($sourceImage); /* offset of drop shadow from top left */ define("DS_OFFSET", 5); /* number of steps from black to background color */ define("DS_STEPS", 10); /* distance between steps */ define("DS_SPREAD", 1); /* define the background color */ $background = array("r" => 255, "g" => 255, "b" => 255); /* create a new canvas. New canvas dimensions should be larger than the original's */ $width = $tWidth+DS_OFFSET; $height = $tHeight+DS_OFFSET; $image = imagecreatetruecolor($width, $height); /* determine the offset between colors */ $step_offset = array("r" => ($background["r"] / DS_STEPS), "g" => ($background["g"] / DS_STEPS), "b" => ($background["b"] / DS_STEPS)); /* calculate and allocate the needed colors */ $current_color = $background; for ($i = 0; $i <= DS_STEPS; $i++) { $colors[$i] = imagecolorallocate($image, round($current_color["r"]), round($current_color["g"]), round($current_color["b"])); $current_color["r"] -= $step_offset["r"]; $current_color["g"] -= $step_offset["g"]; $current_color["b"] -= $step_offset["b"]; } /* floodfill the canvas with the background color */ imagefilledrectangle($image, 0,0, $width, $height, $colors[0]); /* draw overlapping rectangles to create a drop shadow effect */ for ($i = 0; $i < count($colors); $i++) { imagefilledrectangle($image, DS_OFFSET, DS_OFFSET, $width, $height, $colors[$i]); $width -= DS_SPREAD; $height -= DS_SPREAD; } /* overlay the original image on top of the drop shadow */ imagecopymerge($image, $thumb, 0,0, 0,0, $tWidth, $tHeight, 100); imagedestroy($thumb); return $image; } Any help is greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/188405-resize-image-and-add-dropshadow-problems/ Share on other sites More sharing options...
The Little Guy Posted January 14, 2010 Share Posted January 14, 2010 You can add a drop shadow to it using CSS if you wanted, might make it easier.... But that is up to you, otherwise, you should use imagemagick if you have access to it, it will make this also much easier too. another suggestion, is that you could check out a function I made: http://beta.phpsnips.com/snippet.php?id=5 it doesn't do drop shadows though... Link to comment https://forums.phpfreaks.com/topic/188405-resize-image-and-add-dropshadow-problems/#findComment-994637 Share on other sites More sharing options...
tail Posted January 14, 2010 Author Share Posted January 14, 2010 Thanks, I ended up creating two separate functions and it's working now. Thank you for the advice. Link to comment https://forums.phpfreaks.com/topic/188405-resize-image-and-add-dropshadow-problems/#findComment-994639 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.