protected function _resize_padded($canvas_w, $canvas_h, $canvas_color, $width, $height, $orig_w, $orig_h, $origin_x, $origin_y) { $src_x = $src_y = 0; $src_w = $orig_w; $src_h = $orig_h; $cmp_x = $orig_w / $width; $cmp_y = $orig_h / $height; // calculate x or y coordinate and width or height of source if ($cmp_x > $cmp_y) { $src_w = round($orig_w / $cmp_x * $cmp_y); $src_x = round(($orig_w - $orig_w / $cmp_x * $cmp_y) / 2); } else { if ($cmp_y > $cmp_x) { $src_h = round($orig_h / $cmp_y * $cmp_x); $src_y = round(($orig_h - $orig_h / $cmp_y * $cmp_x) / 2); } } $resized = wp_imagecreatetruecolor($canvas_w, $canvas_h); if ($canvas_color === 'transparent') { $color = imagecolorallocatealpha($resized, 255, 255, 255, 127); } else { $rgb = cnColor::rgb2hex2rgb($canvas_color); $color = imagecolorallocatealpha($resized, $rgb['red'], $rgb['green'], $rgb['blue'], 0); } // Fill the background of the new image with allocated color. imagefill($resized, 0, 0, $color); // Restore transparency. imagesavealpha($resized, TRUE); imagecopyresampled($resized, $this->image, $origin_x, $origin_y, $src_x, $src_y, $width, $height, $src_w, $src_h); if (is_resource($resized)) { $this->update_size($width, $height); return $resized; } return new WP_Error('image_resize_error', __('Image resize failed.', 'connections'), $this->file); }
/** * Parses a TimThumb compatible URL via the CN_IMAGE_ENDPOINT root rewrite endpoint * and stream the image to the browser with the correct headers for the image type being served. * * Streams an image resource to the browser or a error message. * * @access private * @since 8.1 * @static * * @uses get_query_var() * @uses path_is_absolute() * @uses cnColor::rgb2hex2rgb() * @uses self::get() * @uses is_wp_error() */ public static function query() { /** @var wpdb $wpdb */ global $wpdb; if (get_query_var(CN_IMAGE_ENDPOINT)) { if (path_is_absolute(get_query_var('src'))) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request'); echo '<h1>ERROR/s:</h1><ul><li>Source is file path. Source must be a local file URL.</li></ul>'; exit; } $atts = array(); if (get_query_var('cn-entry-slug')) { $sql = $wpdb->prepare('SELECT slug FROM ' . CN_ENTRY_TABLE . ' WHERE slug=%s', get_query_var('cn-entry-slug')); $result = $wpdb->get_var($sql); if (is_null($result)) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request'); echo '<h1>ERROR/s:</h1><ul><li>Cheating?</li></ul>'; exit; } else { $atts['sub_dir'] = $result; } } if (get_query_var('w')) { $atts['width'] = get_query_var('w'); } if (get_query_var('h')) { $atts['height'] = get_query_var('h'); } if (get_query_var('zc') || get_query_var('zc') === '0') { $atts['crop_mode'] = get_query_var('zc'); } if (get_query_var('a')) { $atts['crop_focus'] = array('center', 'center'); if (strpos(get_query_var('a'), 't') !== FALSE) { $atts['crop_focus'][1] = 'top'; } if (strpos(get_query_var('a'), 'r') !== FALSE) { $atts['crop_focus'][0] = 'right'; } if (strpos(get_query_var('a'), 'b') !== FALSE) { $atts['crop_focus'][1] = 'bottom'; } if (strpos(get_query_var('a'), 'l') !== FALSE) { $atts['crop_focus'][0] = 'left'; } $atts['crop_focus'] = implode(',', $atts['crop_focus']); } if (get_query_var('f')) { $filters = explode('|', get_query_var('f')); foreach ($filters as $filter) { $param = explode(',', $filter); for ($i = 0; $i < 4; $i++) { if (!isset($param[$i])) { $param[$i] = NULL; } else { $param[$i] = $param[$i]; } } switch ($param[0]) { case '1': $atts['negate'] = TRUE; break; case '2': $atts['grayscale'] = TRUE; break; case '3': $atts['brightness'] = $param[1]; break; case '4': $atts['contrast'] = $param[1]; break; case '5': $atts['colorize'] = cnColor::rgb2hex2rgb($param[1] . ',' . $param[2] . ',' . $param[3]); break; case '6': $atts['detect_edges'] = TRUE; break; case '7': $atts['emboss'] = TRUE; break; case '8': $atts['gaussian_blur'] = TRUE; break; case '9': $atts['blur'] = TRUE; break; case '10': $atts['sketchy'] = TRUE; break; case '11': $atts['smooth'] = $param[1]; } } } if (get_query_var('s') && get_query_var('s') === '1') { $atts['sharpen'] = TRUE; } if (get_query_var('o')) { $atts['opacity'] = get_query_var('o'); } if (get_query_var('q')) { $atts['quality'] = get_query_var('q'); } if (get_query_var('cc')) { $atts['canvas_color'] = get_query_var('cc'); } // This needs to be set after the `cc` query var because it should override any value set using the `cc` query var, just like TimThumb. if (get_query_var('ct') && get_query_var('ct') === '1') { $atts['canvas_color'] = 'transparent'; } // Process the image. $image = self::get(get_query_var('src'), $atts, 'editor'); // If there been an error if (is_wp_error($image)) { $errors = implode('</li><li>', $image->get_error_messages()); // Display the error messages. header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request'); echo '<h1>ERROR/s:</h1><ul><li>' . wp_kses_post($errors) . '</li></ul>'; exit; } // Ensure a stream quality is set otherwise we get a block mess as an image when serving a cached image. $quality = get_query_var('q') ? get_query_var('q') : 90; // Ensure valid value for $quality. If invalid valid is supplied reset to the default of 90, matching WP core. if (filter_var((int) $quality, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 100))) === FALSE) { $quality = 90; } $image->set_quality($quality); $image->stream(); exit; } }