Ok, so I have a slideshow that's 550/350 (w/h in pixels) and I have images varying in size and aspect-ratio... I need to set them to fill the slideshow regardless of size based on height or width and to trim edges 'til it gets there, rather than just popping into the top-left corner.
I can't give an example of my page because it's a half-coded jumbled mess at the moment... but here is some of my code... I'm using the Ultimate Fade-in Slideshow v2.4 from Dynamic Drive (haven't touched the default settings in any of the JS) and this is my CSS:
Here's something I put together a couple years ago or more that you might be able to use:
PHP Code:
/**
* Resize image to specific dimension, cropping as needed
* @return resource Resized image resource, or boolean false on failure
* @param string $imgFile Path to image to be resized
* @param int $width
* @param int $height
* @param string $error Error message
*/
function resize($imgFile, $width, $height, &$error = null)
{
$attrs = @getimagesize($imgFile);
if($attrs == false or $attrs[2] != IMG_JPEG)
{
$error = "Uploaded image is not JPEG or is not readable by this page.";
return false;
}
if($attrs[0] * $attrs[1] > 3000000)
{
$error = "Max pixels allowed is 3,000,000. Your {$attrs[0]} x " .
"{$attrs[1]} image has " . $attrs[0] * $attrs[1] . " pixels.";
return false;
}
$ratio = (($attrs[0] / $attrs[1]) < ($width / $height)) ?
$width / $attrs[0] : $height / $attrs[1];
$x = max(0, round($attrs[0] / 2 - ($width / 2) / $ratio));
$y = max(0, round($attrs[1] / 2 - ($height / 2) / $ratio));
$src = imagecreatefromjpeg($imgFile);
if($src == false)
{
$error = "Unknown problem trying to open uploaded image.";
return false;
}
$resized = imagecreatetruecolor($width, $height);
$result = imagecopyresampled($resized, $src, 0, 0, $x, $y, $width, $height,
round($width / $ratio, 0), round($height / $ratio));
if($result == false)
{
$error = "Error trying to resize and crop image.";
return false;
}
else
{
return $resized;
}
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
I've tested a few options that I've found around the internet and I finally found something that almost works for me but I'm getting an image-not-found error, even though the image does exist and the link is correct... I'm guessing it's because the image is at "http://...." instead of navigating by directory? The image is on another website -- same server, different domain.
I'm also not sure how to return the resized-and-cropped image when it's all done... This function was sort of Jerry-rigged together.
Any help would be greatly appreciated! Thanks!
Here is what I have currently...
PHP Code:
function resizeImg($filename, $targetwidth, $targetheight){
//getting the image dimensions list($width, $height) = getimagesize($image);
//create image from the jpeg $myImage = imagecreatefromjpeg($image) or die("<h3 style=\"color:#FFF;\">Error: Cannot find image: ".$filename." !</h3>");
$diffW = ($width - $targetwidth); $diffH = ($height - $targetheight); if($diffW > $diffH){ // Width is the "overlap" side -- so set for height $sizeDiff = ($height / $targetheight); $sizePost = ($sizeDiff - $targetheight); $cropThis = ($sizePost - $targetwidth)/2; $cropWidth = $cropThis; $cropHeight = NULL; }else{ // Height is the "overlap" side -- so set for width $sizeDiff = ($width / $targetwidth); $sizePost = ($sizeDiff - $targetwidth); $cropThis = ($sizePost - $targetheight)/2; $cropWidth = NULL; $cropHeight = $cropThis; } //if($width > $height) $biggestSide = $width; //find biggest length //else $biggestSide = $height;
//The crop size will be half that of the largest side
//getting the top left coordinate $xCoord = ($width-$cropWidth)/2; $yCoord = ($height-$cropHeight)/2;
// will create a thumbnail image $thethumb = imagecreatetruecolor($targetwidth, $targetheight);
Also, I need it to return the cropped image... this function is stored in a variable (like a $mynewimage = resizeImg($file,$width,$height); sort of thing, then I use $mynewimage later)
If in fact $filename is a URL, it may fail for a number of reasons, including your host having the url_fopen setting disabled (for security reasons). In that case you may have to use the cURL functions (or maybe FTP functions?) to fetch the data via HTTP, save it in a file, then pass the local path to that file to your image function.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
If in fact $filename is a URL, it may fail for a number of reasons, including your host having the url_fopen setting disabled (for security reasons). In that case you may have to use the cURL functions (or maybe FTP functions?) to fetch the data via HTTP, save it in a file, then pass the local path to that file to your image function.
The FTP solution would probably be better, with the caveat that you would have to have FTP access to the location where the image file is. If you only have HTTP access (i.e. via URL), then you'd have to go with the cURL option.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks