Ticket #15311: vt_resize.txt

File vt_resize.txt, 3.5 KB (added by daltonrooney, 12 months ago)

Small update to predict final image size before naming it.

Line 
1<?php
2/*
3 * Resize images dynamically using wp built in functions
4 * Victor Teixeira
5 *
6 * php 5.2+
7 *
8 * Exemplo de uso:
9 *
10 * <?php
11 * $thumb = get_post_thumbnail_id();
12 * $image = vt_resize( $thumb, '', 140, 110, true );
13 * ?>
14 * <img src="<?php echo $image[url]; ?>" width="<?php echo $image[width]; ?>" height="<?php echo $image[height]; ?>" />
15 *
16 * @param int $attach_id
17 * @param string $img_url
18 * @param int $width
19 * @param int $height
20 * @param bool $crop
21 * @return array
22 */
23
24function vt_resize( $attach_id = null, $img_url = null, $width, $height, $crop = false ) {
25
26        // this is an attachment, so we have the ID
27        if ( $attach_id ) {
28       
29                $image_src = wp_get_attachment_image_src( $attach_id, 'full' );
30                $file_path = get_attached_file( $attach_id );
31       
32        // this is not an attachment, let's use the image url
33        } else if ( $img_url ) {
34               
35                $file_path = parse_url( $img_url );
36                $file_path = $_SERVER['DOCUMENT_ROOT'] . $file_path['path'];
37               
38                //$file_path = ltrim( $file_path['path'], '/' );
39                //$file_path = rtrim( ABSPATH, '/' ).$file_path['path'];
40               
41                $orig_size = getimagesize( $file_path );
42               
43                $image_src[0] = $img_url;
44                $image_src[1] = $orig_size[0];
45                $image_src[2] = $orig_size[1];
46        }
47       
48        $file_info = pathinfo( $file_path );
49        $extension = '.'. $file_info['extension'];
50
51        // the image path without the extension
52        $no_ext_path = $file_info['dirname'].'/'.$file_info['filename'];
53       
54        /* Calculate the eventual height and width for accurate file name */
55
56        if ( $crop == false ) {
57                $proportional_size = wp_constrain_dimensions( $image_src[1], $image_src[2], $width, $height );
58                $width = $proportional_size[0];
59                $height = $proportional_size[1];
60        }
61       
62        $cropped_img_path = $no_ext_path.'-'.$width.'x'.$height.$extension;
63
64        // checking if the file size is larger than the target size
65        // if it is smaller or the same size, stop right here and return
66        if ( $image_src[1] > $width || $image_src[2] > $height ) {
67
68                // the file is larger, check if the resized version already exists (for $crop = true but will also work for $crop = false if the sizes match)
69                if ( file_exists( $cropped_img_path ) ) {
70
71                        $cropped_img_url = str_replace( basename( $image_src[0] ), basename( $cropped_img_path ), $image_src[0] );
72                       
73                        $vt_image = array (
74                                'url' => $cropped_img_url,
75                                'width' => $width,
76                                'height' => $height
77                        );
78                       
79                        return $vt_image;
80                }
81
82                // $crop = false
83                if ( $crop == false ) {
84               
85                        // calculate the size proportionaly
86                        $proportional_size = wp_constrain_dimensions( $image_src[1], $image_src[2], $width, $height );
87                        $resized_img_path = $no_ext_path.'-'.$proportional_size[0].'x'.$proportional_size[1].$extension;                       
88
89                        // checking if the file already exists
90                        if ( file_exists( $resized_img_path ) ) {
91                       
92                                $resized_img_url = str_replace( basename( $image_src[0] ), basename( $resized_img_path ), $image_src[0] );
93
94                                $vt_image = array (
95                                        'url' => $resized_img_url,
96                                        'width' => $proportional_size[0],
97                                        'height' => $proportional_size[1]
98                                );
99                               
100                                return $vt_image;
101                        }
102                }
103
104                // no cache files - let's finally resize it
105                $new_img_path = image_resize( $file_path, $width, $height, $crop );
106                $new_img_size = getimagesize( $new_img_path );
107                $new_img = str_replace( basename( $image_src[0] ), basename( $new_img_path ), $image_src[0] );
108
109                // resized output
110                $vt_image = array (
111                        'url' => $new_img,
112                        'width' => $new_img_size[0],
113                        'height' => $new_img_size[1]
114                );
115               
116                return $vt_image;
117        }
118
119        // default output - without resizing
120        $vt_image = array (
121                'url' => $image_src[0],
122                'width' => $image_src[1],
123                'height' => $image_src[2]
124        );
125       
126        return $vt_image;
127}