Welcome Guest, Not a member yet? Register   Sign In
Image cropping challenge
#3

[eluser]Greendots[/eluser]
Think this might do the trick. (Credited original author)

Code:
//Author Alan Reddan Silverarm Solutions
        //Date 27/01/2005
        //Function that works well with images.
        //It takes the image and reduces its size to best fit. i.e If you have an image
        //that is 200 X 100 and you want a thumbnail of 75 X 50,
        //it first resizes the image to 100 X 50
        //and then takes out a portion 75 X 50 from then center of the input image.
        //So loads of image information is retained.
        //The corollary also holds if your input image is 100 X 200
        //it first resizes image to 75 X 150 and then takes out a
        //portion 75 X 75 from the centre
        // The advantage here is that function decides on whether
        //resize is by width or height itself.
        //it also decides whether to use the height or the width as the base start point
        //in the case that athumbnail is rectangular

        function resize_then_crop( $filein,$fileout,$extension,$imagethumbsize_w,$imagethumbsize_h,$red,$green,$blue)
        {
        
        $percent = 0.5;
        // Get new dimensions
        list($width, $height) = getimagesize($filein);
        $new_width = $width * $percent;
        $new_height = $height * $percent;
        
        
        switch ($extension)
        {
            case '.jpg': case '.JPG': case '.JPEG': case '.jpeg':
                $format = 'image/jpeg';
                $image = imagecreatefromjpeg($filein);
            break;
        
            case '.gif': case '.GIF':
                $format = 'image/gif';
                $image = imagecreatefromgif($filein);
              break;
                
            case '.png': case '.PNG':
                $format = 'image/png';
                $image = imagecreatefrompng($filein);
            break;
        }
        
        $width = $imagethumbsize_w ;
        $height = $imagethumbsize_h ;
        list($width_orig, $height_orig) = getimagesize($filein);
        
        if ($width_orig < $height_orig) {
          $height = ($imagethumbsize_w / $width_orig) * $height_orig;
        } else {
            $width = ($imagethumbsize_h / $height_orig) * $width_orig;
        }
        
        if ($width < $imagethumbsize_w)
        //if the width is smaller than supplied thumbnail size
        {
        $width = $imagethumbsize_w;
        $height = ($imagethumbsize_w/ $width_orig) * $height_orig;;
        }
        
        if ($height < $imagethumbsize_h)
        //if the height is smaller than supplied thumbnail size
        {
        $height = $imagethumbsize_h;
        $width = ($imagethumbsize_h / $height_orig) * $width_orig;
        }
        
        $thumb = imagecreatetruecolor($width , $height);
        $bgcolor = imagecolorallocate($thumb, $red, $green, $blue);  
        ImageFilledRectangle($thumb, 0, 0, $width, $height, $bgcolor);
        imagealphablending($thumb, true);
        
        imagecopyresampled($thumb, $image, 0, 0, 0, 0,
        $width, $height, $width_orig, $height_orig);
        $thumb2 = imagecreatetruecolor($imagethumbsize_w , $imagethumbsize_h);
        // true color for best quality
        $bgcolor = imagecolorallocate($thumb2, $red, $green, $blue);  
        //ImageFilledRectangle($thumb2, 0, 0,    $imagethumbsize_w , $imagethumbsize_h , $white);
        ImageFilledRectangle($thumb2, 0, 0,    $imagethumbsize_w , $imagethumbsize_h , $bgcolor);
        imagealphablending($thumb2, true);
        
        $w1 =($width/2) - ($imagethumbsize_w/2);
        $h1 = ($height/2) - ($imagethumbsize_h/2);
        
        imagecopyresampled($thumb2, $thumb, 0,0, $w1, $h1,
        $imagethumbsize_w , $imagethumbsize_h ,$imagethumbsize_w, $imagethumbsize_h);
        
        //Error, this will always return true.
        
        // Output
        //header('Content-type: image/gif');
        //imagegif($thumb); //output to browser first image when testing
        
        switch ($extension)
        {
            case '.jpg': case '.JPG': case '.JPEG': case '.jpeg':
                imagejpeg($thumb2, $fileout,100);
            break;
        
            case '.gif': case '.GIF':
                imagegif($thumb2, $fileout) ;
              break;
                
            case '.png': case '.PNG':
                imagepng($thumb2, $fileout, 100);
            break;
        }
        
    
        return true ;
        //header('Content-type: image/gif');
        //imagegif($thumb2); //output to browser
        }


Messages In This Thread
Image cropping challenge - by El Forum - 02-14-2009, 08:40 AM
Image cropping challenge - by El Forum - 02-14-2009, 10:11 AM
Image cropping challenge - by El Forum - 02-14-2009, 10:11 AM
Image cropping challenge - by El Forum - 02-14-2009, 10:14 AM
Image cropping challenge - by El Forum - 02-14-2009, 10:21 AM
Image cropping challenge - by El Forum - 02-15-2009, 10:09 AM
Image cropping challenge - by El Forum - 02-15-2009, 10:26 AM
Image cropping challenge - by El Forum - 02-16-2009, 12:22 AM
Image cropping challenge - by El Forum - 02-16-2009, 12:27 AM
Image cropping challenge - by El Forum - 02-16-2009, 12:32 AM
Image cropping challenge - by El Forum - 02-16-2009, 12:44 AM
Image cropping challenge - by El Forum - 02-16-2009, 12:46 AM
Image cropping challenge - by El Forum - 02-22-2009, 08:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB