www.webdeveloper.com
Results 1 to 7 of 7

Thread: imagecopy crop-image edges help?

  1. #1
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    264

    Exclamation imagecopy crop-image edges help?

    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:

    Code:
    .header #slideshow{
    	display:block;
    	display:inline-block;
    	margin:0 0 -1px auto;
    	float:right;
    	width:550px;
    	height:350px;
    	text-align:center;
    	border:rgb(134,137,71) 1px solid;
    }
    .header #slideshow img{
    	display:block;
    	height:380px;
    	margin: 0 auto;
    	text-align:center;
    	padding:0;
    }
    So, for now it just stretches it to 30px taller than the box, but some of the images are taller than they are wide.

    ...any advice would be greatly appreciated -- Thanks!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    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(0round($attrs[0] / - ($width 2) / $ratio));
       
    $y max(0round($attrs[1] / - ($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$src00$x$y$width$height,
                 
    round($width $ratio0), 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

    eBookworm.us

  3. #3
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    264
    For some reason it didn't work for me.

    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); 

        
    imagecopyresampled($thethumb$myImage00,$xCoord$yCoord$targetwidth$targetheight$cropWidth$cropHeight); 

                         
        
    header('Content-type: image/jpeg');
        
    imagejpeg($thethumb);
        
    imagedestroy($thethumb); 

    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)

  4. #4
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    264
    The Error message I get is the one from this line:

    PHP Code:
    $myImage imagecreatefromjpeg($filename) or die("<h3 style=\"color:#FFF;\">Error: Cannot find image: ".$filename." !</h3>"); 
    ...but the photo DOES exist, and it's a .jpg file.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    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

    eBookworm.us

  6. #6
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    264
    Quote Originally Posted by NogDog View Post
    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.
    Any advice as to how I might accomplish that?

    (Thanks!)

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Best I can do right now is point you to the manual: www.php.net/cURL and www.php.net/FTP.

    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

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles