Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * All image functionality
- * available hook :
- * - jeg_register_image_size (register / modify another image size)
- *
- * @author jegbagus
- */
- /**
- * initalize image size
- */
- add_action('init', 'jeg_theme_image_sizes');
- function jeg_theme_image_sizes()
- {
- $imageDim = array (
- "post-feature-image" => array(602, 300)
- );
- $imageDim = apply_filters('jeg_register_image_size', $imageDim);
- if (function_exists('add_theme_support'))
- {
- add_theme_support ( 'post-thumbnails' );
- foreach($imageDim as $id => $val) {
- add_image_size($id, $val[0], $val[1], true);
- }
- }
- return $imageDim;
- }
- /**
- * get post thumbnail image, and return array of attribute
- **/
- function j_get_post_header_image($post, $size){
- $attachment_id = get_post_thumbnail_id($post->ID);
- $image = wp_get_attachment_image_src($attachment_id, $size);
- if ( $image ) {
- list($src, $width, $height) = $image;
- $attachment =& get_post($attachment_id);
- $default_attr = array(
- 'src' => $src,
- 'class' => "attachment-$size",
- 'alt' => trim(strip_tags( get_the_title() ))
- );
- if ( empty($default_attr['alt']) )
- $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
- if ( empty($default_attr['alt']) )
- $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
- return $default_attr;
- } else {
- return NULL;
- }
- }
- /**
- * Build image tag from array
- *
- * @param array $arrs
- */
- function j_build_image ($arrs)
- {
- $html = "<img ";
- foreach( $arrs as $name => $value ) {
- $html .= " $name=" . '"' . $value . '"';
- }
- $html .= " />";
- return $html;
- }
- /**
- * get image after resizing
- *
- * @param $img image url
- * @param $dim defined image dimension
- * @param $w image width
- * @param $h image height
- * @param $a align
- * @param $zc zoom crop
- */
- function jeg_get_image($img = '', $dim = '', $w = false, $h = false , $nocrop = false , $fillcolor = "000000")
- {
- if($dim != '')
- {
- $imageDim = jeg_get_theme_image_sizes();
- if(isset($imageDim[$dim]))
- {
- $w = $imageDim[$dim][0];
- $h = $imageDim[$dim][1];
- }
- }
- return jeg_image_resizer($img, $w, $h, $nocrop, $fillcolor);
- }
- /**
- * Get image meta description for image ( basedir , baseurl , imgpath , extension , height , width , destpath , imagetype)
- *
- * @param string $source URL source of image
- * @return array of image meta description
- */
- function jeg_get_image_meta ($source)
- {
- // meta image container
- $meta = array();
- // define upload path & dir
- $upload_info = wp_upload_dir();
- $meta['basedir'] = $upload_info['basedir'];
- $meta['baseurl'] = $upload_info['baseurl'];
- // check if image is local
- if(strpos( $source, $meta['baseurl'] ) === false)
- {
- return false;
- } else
- {
- $destpath = str_replace( $meta['baseurl'], '', $source);
- $meta['imgpath'] = $meta['basedir'] . $destpath;
- // check if img path exists, and is an image indeed
- if( !file_exists($meta['imgpath']) OR !getimagesize($meta['imgpath']) )
- {
- return false;
- } else
- {
- // get image info
- $info = pathinfo($meta['imgpath']);
- $meta['extension'] = $info['extension'];
- // get image size
- $imagesize = getimagesize($meta['imgpath']);
- $meta['width'] = $imagesize[0];
- $meta['height'] = $imagesize[1];
- $meta['imagetype'] = image_type_to_mime_type($imagesize[2]);
- // now define dest path
- $meta['destpath'] = str_replace( '.' . $meta['extension'], '' , $destpath);
- // return this meta
- return $meta;
- }
- }
- }
- /**
- * Jeg Custom Resizer function
- *
- * @param string $source
- * @param int $w
- * @param int $h
- * @param boolean $crop
- * @param string of color hex
- */
- function jeg_image_resizer ($source, $w, $h, $nocrop, $fillcolor)
- {
- $meta = jeg_get_image_meta($source);
- $imgpath = '';
- if($meta)
- {
- if ($nocrop) {
- // show all image without croping zone
- $imgpath = jeg_resize_nocrop($meta, $w, $h, $fillcolor);
- } else {
- // show image with croping zone to fill container
- $imgpath = jeg_resize_crop($meta, $w, $h);
- }
- return $imgpath;
- } else {
- return false;
- }
- }
- /**
- * Show all image without croping zone
- *
- * @param array of $meta image
- * @param int $w
- * @param int $h
- * @param string hext of color
- * @param int $quality
- */
- function jeg_resize_nocrop($meta, $w, $h, $fillcolor, $quality = 100)
- {
- /*
- * Do some math to figure out which way we'll need to crop the image
- * to get it proportional to the new size, then crop or adjust as needed
- */
- $x_ratio = $w / $meta['width'];
- $y_ratio = $h / $meta['height'];
- if (($meta['width'] <= $w) && ($meta['height'] <= $h)) {
- $new_w = $meta['width'];
- $new_h = $meta['height'];
- } elseif (($x_ratio * $meta['height']) < $h) {
- $new_h = ceil($x_ratio * $meta['height']);
- $new_w = $w;
- } else {
- $new_w = ceil($y_ratio * $meta['width']);
- $new_h = $h;
- }
- $destination = $meta['basedir'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '-nocrop' . '.' . $meta['extension'] ;
- $desturl = $meta['baseurl'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '-nocrop' . '.' . $meta['extension'] ;
- // check if file exist
- if( file_exists($destination) ) {
- return $desturl;
- }
- switch ($meta['imagetype']) {
- case 'image/jpeg':
- $source = imagecreatefromjpeg($meta['imgpath']);
- break;
- case 'image/gif':
- $source = imagecreatefromgif($meta['imgpath']);
- break;
- case 'image/png':
- $source = imagecreatefrompng($meta['imgpath']);
- break;
- default:
- return;
- }
- // do resizing and add background to fill
- $newpic = imagecreatetruecolor(round($new_w), round($new_h));
- imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $meta['width'], $meta['height']);
- $final = imagecreatetruecolor($w, $h);
- $fillbg = jeg_hex2RGB($fillcolor);
- $backgroundColor = imagecolorallocate($final, $fillbg['red'], $fillbg['green'], $fillbg['blue']);
- imagefill($final, 0, 0, $backgroundColor);
- imagecopy($final, $newpic, (($w - $new_w)/ 2), (($h - $new_h) / 2), 0, 0, $new_w, $new_h);
- if (imagejpeg($final, $destination, $quality)) {
- return $desturl;
- }
- return false;
- }
- /**
- * Build image that will fill the container
- *
- * @todo : check kalau image lebih kecil, maka pake jeg_resize_nocrop
- *
- * @param array $meta of meta image
- * @param int $w
- * @param int $h
- * @return string of url path
- */
- function jeg_resize_crop ($meta, $w, $h)
- {
- //get image size after cropping
- $dims = image_resize_dimensions($meta['width'], $meta['height'], $w, $h, true);
- $new_w = $dims[4];
- $new_h = $dims[5];
- //use this to check if cropped image already exists, so we can return that instead
- $destination = $meta['basedir'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '.' . $meta['extension'] ;
- $desturl = $meta['baseurl'] . $meta['destpath'] . '-' . $new_w . 'x' . $new_h . '.' . $meta['extension'] ;
- // check if file is exist
- if(file_exists($destination) && getimagesize($destination)) {
- return $desturl;
- } else {
- $resized_img_path = jeg_image_resize_hub( $meta['imgpath'], $new_w, $new_h, true );
- $resized_rel_path = str_replace( $meta['basedir'], '', $resized_img_path);
- return ( $meta['baseurl'] . $resized_rel_path );
- }
- return $desturl;
- }
- /**
- * this function allow user to use themes bellow version 3.4
- */
- function jeg_image_resize_hub($img_path, $width, $height, $crop = false) {
- $resized_img_path = '';
- if(function_exists('wp_get_image_editor')) {
- $image = wp_get_image_editor($img_path);
- $image->resize($width, $height, $crop);
- $resized_img_path = $image->generate_filename();
- $image->save($resized_img_path);
- } else {
- $resized_img_path = image_resize( $img_path, $width, $height, $crop );
- }
- return $resized_img_path;
- }
Advertisement
Add Comment
Please, Sign In to add comment