/** * Test {@link scale_to_constraint()} */ function test_scale_to_constraint() { $this->assertEqual(scale_to_constraint(50, 50, 20, 20), array(20, 20)); $this->assertEqual(scale_to_constraint(50, 100, 100, 10), array(5, 10)); // Tests with NULL $this->assertEqual(scale_to_constraint(50, 70, 50, NULL), array(50, 70)); $this->assertEqual(scale_to_constraint(50, 70, 25, NULL), array(25, 35)); $this->assertEqual(scale_to_constraint(50, 70, NULL, 35), array(25, 35)); $this->assertEqual(scale_to_constraint(50, 70, NULL, NULL), array(50, 70)); }
/** * Generate a thumbnail * * @return array short error code + dest image handler */ function generate_thumb($src_imh, $thumb_width, $thumb_height) { $src_width = imagesx($src_imh); $src_height = imagesy($src_imh); if ($src_width <= $thumb_width && $src_height <= $thumb_height) { // There is no need to resample, use original! return array(NULL, $src_imh); } list($dest_width, $dest_height) = scale_to_constraint($src_width, $src_height, $thumb_width, $thumb_height); // pre_dump( $src_width, $src_height, $dest_width, $dest_height ); $dest_imh = imagecreatetruecolor($dest_width, $dest_height); // Create a black image if (!imagecopyresampled($dest_imh, $src_imh, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height)) { return array('!GD-library internal error (resample)', $dest_imh); } // TODO: imageinterlace(); return array(NULL, $dest_imh); }
/** * Generate a thumbnail * * @param resource Image resource * @param string Thumbnail type ('crop'|'fit') * @param int Thumbnail width * @param int Thumbnail height * @param int Thumbnail percent of blur effect (0 - No blur, 1% - Max blur effect, 99% - Min blur effect) * @return array short error code + dest image handler */ function generate_thumb($src_imh, $thumb_type, $thumb_width, $thumb_height, $thumb_percent_blur = 0) { $src_width = imagesx($src_imh); $src_height = imagesy($src_imh); if ($src_width <= $thumb_width && $src_height <= $thumb_height) { // There is no need to resample, use original! return array(NULL, $src_imh); } switch ($thumb_type) { case 'crop': case 'crop-top': $align = $thumb_type == 'crop-top' ? 'top' : 'center'; list($src_x, $src_y, $src_width, $src_height) = crop_to_constraint($src_width, $src_height, $thumb_width, $thumb_height, $align); $dest_width = $thumb_width; $dest_height = $thumb_height; break; case 'fit': default: list($dest_width, $dest_height) = scale_to_constraint($src_width, $src_height, $thumb_width, $thumb_height); $src_x = $src_y = 0; } // pre_dump( $src_x, $src_y, $dest_width, $dest_height, $src_width, $src_height ); // Create a transparent image: $dest_imh = imagecreatetruecolor($dest_width, $dest_height); imagealphablending($dest_imh, true); imagefill($dest_imh, 0, 0, imagecolortransparent($dest_imh, imagecolorallocatealpha($dest_imh, 0, 0, 0, 127))); imagesavealpha($dest_imh, true); if (!imagecopyresampled($dest_imh, $src_imh, 0, 0, $src_x, $src_y, $dest_width, $dest_height, $src_width, $src_height)) { return array('!GD-library internal error (resample)', $dest_imh); } if ($thumb_percent_blur > 0) { // Apply blur effect $dest_imh = pixelblur($dest_imh, $dest_width, $dest_height, $thumb_percent_blur); } // TODO: imageinterlace(); return array(NULL, $dest_imh); }
/** * Calculate what sizes are used for thumbnail really * * @param string Thumbnail size name * @return boolean|array FALSE on wrong size name OR Array with keys: 0 - width, 1 - height */ function get_thumb_size($size_name = 'fit-80x80') { global $thumbnail_sizes; if (!isset($thumbnail_sizes[$size_name])) { // Wrong thumbnail size name return false; } $thumb_type = $thumbnail_sizes[$size_name][0]; $thumb_width = $thumbnail_sizes[$size_name][1]; $thumb_height = $thumbnail_sizes[$size_name][2]; load_funcs('files/model/_image.funcs.php'); list($orig_width, $orig_height) = $this->get_image_size('widthheight'); if (check_thumbnail_sizes($thumb_type, $thumb_width, $thumb_height, $orig_width, $orig_height)) { // Use the sizes of the original image $width = $orig_width; $height = $orig_height; } else { // Calculate the sizes depending on thumbnail type if ($thumb_type == 'fit') { list($width, $height) = scale_to_constraint($orig_width, $orig_height, $thumb_width, $thumb_height); } else { // crop & crop-top $width = $thumb_width; $height = $thumb_height; } } return array($width, $height); }