public static function create_favicon($dir_name, $image, $sizes = false) { it_classes_load('it-image-utility.php'); return ITImageUtility::create_favicon($dir_name, $image, $sizes); }
public static function resize_image($file, $max_w = 0, $max_h = 0, $crop = true, $suffix = null, $dest_path = null, $jpeg_quality = 90) { it_classes_load('it-file-utility.php'); if (is_numeric($file)) { $file_info = ITFileUtility::get_file_attachment($file); if (false === $file_info) { return new WP_Error('error_loading_image_attachment', "Could not find requested file attachment ({$file})"); } $file = $file_info['file']; } else { $file_attachment_id = ''; } if (preg_match('/\\.ico$/', $file)) { return array('file' => $file, 'url' => ITFileUtility::get_url_from_file($file), 'name' => basename($file)); } if (version_compare($GLOBALS['wp_version'], '3.4.9', '>')) { // Compat code taken from pre-release 3.5.0 code. if (!file_exists($file)) { return new WP_Error('error_loading_image', sprintf(__('File “%s” doesn’t exist?'), $file)); } if (!function_exists('imagecreatefromstring')) { return new WP_Error('error_loading_image', __('The GD image library is not installed.')); } // Set artificially high because GD uses uncompressed images in memory @ini_set('memory_limit', apply_filters('image_memory_limit', WP_MAX_MEMORY_LIMIT)); $image = imagecreatefromstring(file_get_contents($file)); if (!is_resource($image)) { return new WP_Error('error_loading_image', sprintf(__('File “%s” is not an image.'), $file)); } } else { require_once ABSPATH . 'wp-admin/includes/image.php'; $image = wp_load_image($file); if (!is_resource($image)) { return new WP_Error('error_loading_image', $image); } } list($orig_w, $orig_h, $orig_type) = getimagesize($file); $dims = ITImageUtility::_image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop); if (!$dims) { return new WP_Error('error_resizing_image', "Could not resize image"); } list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; if ($orig_w == $dst_w && $orig_h == $dst_h) { return array('file' => $file, 'url' => ITFileUtility::get_url_from_file($file), 'name' => basename($file)); } if (!$suffix) { $suffix = "resized-image-{$dst_w}x{$dst_h}"; } $info = pathinfo($file); $dir = $info['dirname']; $ext = $info['extension']; $name = basename($file, ".{$ext}"); if (!is_null($dest_path) && ($_dest_path = realpath($dest_path))) { $dir = $_dest_path; } $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}"; if (file_exists($destfilename)) { if (filemtime($file) > filemtime($destfilename)) { unlink($destfilename); } else { return array('file' => $destfilename, 'url' => ITFileUtility::get_url_from_file($destfilename), 'name' => basename($destfilename)); } } // ImageMagick cannot resize animated PNG files yet, so this only works for // animated GIF files. $animated = false; if (ITImageUtility::is_animated_gif($file)) { $coalescefilename = "{$dir}/{$name}-coalesced-file.{$ext}"; if (!file_exists($coalescefilename)) { system("convert {$file} -coalesce {$coalescefilename}"); } if (file_exists($coalescefilename)) { system("convert -crop {$src_w}x{$src_h}+{$src_x}+{$src_y}! {$coalescefilename} {$destfilename}"); if (file_exists($destfilename)) { system("mogrify -resize {$dst_w}x{$dst_h} {$destfilename}"); system("convert -layers optimize {$destfilename}"); $animated = true; } } } if (!$animated) { $newimage = imagecreatetruecolor($dst_w, $dst_h); // preserve PNG transparency if (IMAGETYPE_PNG == $orig_type && function_exists('imagealphablending') && function_exists('imagesavealpha')) { imagealphablending($newimage, false); imagesavealpha($newimage, true); } imagecopyresampled($newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); // we don't need the original in memory anymore if ($orig_type == IMAGETYPE_GIF) { if (!imagegif($newimage, $destfilename)) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } elseif ($orig_type == IMAGETYPE_PNG) { if (!imagepng($newimage, $destfilename)) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } else { // all other formats are converted to jpg $destfilename = "{$dir}/{$name}-{$suffix}.jpg"; if (!imagejpeg($newimage, $destfilename, apply_filters('jpeg_quality', $jpeg_quality))) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } imagedestroy($newimage); } imagedestroy($image); // Set correct file permissions $stat = stat(dirname($destfilename)); $perms = $stat['mode'] & 0666; //same permissions as parent folder, strip off the executable bits @chmod($destfilename, $perms); return array('file' => $destfilename, 'url' => ITFileUtility::get_url_from_file($destfilename), 'name' => basename($destfilename)); }