当前位置: 首页>>代码示例>>PHP>>正文


PHP image_make_intermediate_size函数代码示例

本文整理汇总了PHP中image_make_intermediate_size函数的典型用法代码示例。如果您正苦于以下问题:PHP image_make_intermediate_size函数的具体用法?PHP image_make_intermediate_size怎么用?PHP image_make_intermediate_size使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了image_make_intermediate_size函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: themify_make_image_size

 /**
  * Creates new image size.
  *
  * @uses get_attached_file()
  * @uses image_make_intermediate_size()
  * @uses wp_update_attachment_metadata()
  * @uses get_post_meta()
  * @uses update_post_meta()
  *
  * @param $attachment_id
  * @param $width
  * @param $height
  * @param $meta
  * @param $original_src
  *
  * @return array
  */
 function themify_make_image_size($attachment_id, $width, $height, $meta, $original_src)
 {
     setlocale(LC_CTYPE, get_locale() . '.UTF-8');
     $attached_file = get_attached_file($attachment_id);
     if (apply_filters('themify_image_script_use_large_size', true) && isset($meta['sizes']['large']['file'])) {
         $attached_file = str_replace($meta['file'], trailingslashit(dirname($meta['file'])) . $meta['sizes']['large']['file'], $attached_file);
     }
     $resized = image_make_intermediate_size($attached_file, $width, $height, true);
     if ($resized && !is_wp_error($resized)) {
         // Save the new size in meta data
         $key = sprintf('resized-%dx%d', $width, $height);
         $meta['sizes'][$key] = $resized;
         $src = str_replace(basename($original_src), $resized['file'], $original_src);
         wp_update_attachment_metadata($attachment_id, $meta);
         // Save size in backup sizes so it's deleted when original attachment is deleted.
         $backup_sizes = get_post_meta($attachment_id, '_wp_attachment_backup_sizes', true);
         if (!is_array($backup_sizes)) {
             $backup_sizes = array();
         }
         $backup_sizes[$key] = $resized;
         update_post_meta($attachment_id, '_wp_attachment_backup_sizes', $backup_sizes);
         // Return resized image url, width and height.
         return array('url' => esc_url($src), 'width' => $width, 'height' => $height);
     }
     // Return resized image url, width and height.
     return array('url' => $original_src, 'width' => $width, 'height' => $height);
 }
开发者ID:rgrasiano,项目名称:viabasica-hering,代码行数:44,代码来源:img.php

示例2: circleflip_make_missing_intermediate_size

 /** generate new image sizes for old uploads
  * 
  * @param mixed $out
  * @param int $id
  * @param string|array $size
  * @return mixed
  */
 function circleflip_make_missing_intermediate_size($out, $id, $size)
 {
     $skip_sizes = array('full', 'large', 'medium', 'thumbnail', 'thumb');
     if (is_array($size) || in_array($size, $skip_sizes)) {
         return $out;
     }
     // the size exists and the attachment doesn't have a pre-generated file of that size
     // or the intermediate size defintion changed ( during development )
     if (circleflip_intermediate_size_exists($size) && (!circleflip_image_has_intermediate_size($id, $size) || circleflip_has_intermediate_size_changed($id, $size))) {
         // get info registerd by add_image_size
         $size_info = circleflip_get_intermediate_size_info($size);
         // get path to the original file
         $file_path = get_attached_file($id);
         // resize the image
         $resized_file = image_make_intermediate_size($file_path, $size_info['width'], $size_info['height'], $size_info['crop']);
         if ($resized_file) {
             // we have a resized image, get the attachment metadata and add the new size to it
             $metadata = wp_get_attachment_metadata($id);
             $metadata['sizes'][$size] = $resized_file;
             if (wp_update_attachment_metadata($id, $metadata)) {
                 // update succeded, call image_downsize again , DRY
                 return image_downsize($id, $size);
             }
         } else {
             // failed to generate the resized image
             // call image_downsize with `full` to prevent infinit loop
             return image_downsize($id, 'full');
         }
     }
     // pre-existing intermediate size
     return $out;
 }
开发者ID:purgesoftwares,项目名称:purges,代码行数:39,代码来源:WP-Make-Missing-Sizes.php

示例3: resize

 /**
  * Resizes an image to a specified size
  * @param  integer|string  $originalImage Attachment id, path or url
  * @param  integer         $width         Target width
  * @param  integer         $height        Target height
  * @param  boolean         $crop          Crop or not?
  * @return string                         Image url
  */
 public static function resize($originalImage, $width, $height, $crop = true)
 {
     $imagePath = false;
     // Image from attachment id
     if (is_numeric($originalImage)) {
         $imagePath = wp_get_attachment_url($originalImage);
     } elseif (in_array(substr($originalImage, 0, 7), array('https:/', 'http://'))) {
         $imagePath = self::urlToPath($originalImage);
     }
     if (!$imagePath) {
         return false;
     }
     $imagePath = self::removeImageSize($imagePath);
     if (!file_exists($imagePath)) {
         return false;
     }
     $imagePathInfo = pathinfo($imagePath);
     $ext = $imagePathInfo['extension'];
     $suffix = "{$width}x{$height}";
     $destPath = "{$imagePathInfo['dirname']}/{$imagePathInfo['filename']}-{$suffix}.{$ext}";
     if (file_exists($destPath)) {
         return self::pathToUrl($destPath);
     }
     if (image_make_intermediate_size($imagePath, $width, $height, $crop)) {
         return self::pathToUrl($destPath);
     }
     return $originalImage;
 }
开发者ID:helsingborg-stad,项目名称:Municipio,代码行数:36,代码来源:Image.php

示例4: resizeImage

 public function resizeImage($attachment_id, $width, $height, $crop = true)
 {
     // Get upload directory info
     $upload_info = wp_upload_dir();
     $upload_dir = $upload_info['basedir'];
     $upload_url = $upload_info['baseurl'];
     // Get file path info
     $path = get_attached_file($attachment_id);
     $path_info = pathinfo($path);
     $ext = $path_info['extension'];
     $rel_path = str_replace(array($upload_dir, ".{$ext}"), '', $path);
     $suffix = "{$width}x{$height}";
     $dest_path = "{$upload_dir}{$rel_path}-{$suffix}.{$ext}";
     $url = "{$upload_url}{$rel_path}-{$suffix}.{$ext}";
     // If file exists: do nothing
     if (file_exists($dest_path)) {
         return $url;
     }
     // Generate thumbnail
     if (image_make_intermediate_size($path, $width, $height, $crop)) {
         return $url;
     }
     // Fallback to full size
     return "{$upload_url}{$rel_path}.{$ext}";
 }
开发者ID:helsingborg-stad,项目名称:Municipio,代码行数:25,代码来源:OnTheFlyImages.php

示例5: sp_generate_attachment_metadata

function sp_generate_attachment_metadata($attachment_id, $file)
{
    $attachment = get_post($attachment_id);
    $metadata = array();
    if (preg_match('!^image/!', get_post_mime_type($attachment)) && file_is_displayable_image($file)) {
        $imagesize = getimagesize($file);
        $metadata['width'] = $imagesize[0];
        $metadata['height'] = $imagesize[1];
        list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
        $metadata['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
        // Make the file path relative to the upload dir
        $metadata['file'] = $attachment->post_title;
        // make thumbnails and other intermediate sizes
        global $_wp_additional_image_sizes;
        $temp_sizes = array('thumbnail', 'medium', 'large');
        // Standard sizes
        if (isset($_wp_additional_image_sizes) && count($_wp_additional_image_sizes)) {
            $temp_sizes = array_merge($temp_sizes, array_keys($_wp_additional_image_sizes));
        }
        $temp_sizes = apply_filters('intermediate_image_sizes', $temp_sizes);
        foreach ($temp_sizes as $s) {
            $sizes[$s] = array('width' => '', 'height' => '', 'crop' => FALSE);
            if (isset($_wp_additional_image_sizes[$s]['width'])) {
                $sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']);
            } else {
                $sizes[$s]['width'] = get_option("{$s}_size_w");
            }
            // For default sizes set in options
            if (isset($_wp_additional_image_sizes[$s]['height'])) {
                $sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']);
            } else {
                $sizes[$s]['height'] = get_option("{$s}_size_h");
            }
            // For default sizes set in options
            if (isset($_wp_additional_image_sizes[$s]['crop'])) {
                $sizes[$s]['crop'] = intval($_wp_additional_image_sizes[$s]['crop']);
            } else {
                $sizes[$s]['crop'] = get_option("{$s}_crop");
            }
            // For default sizes set in options
        }
        $sizes = apply_filters('intermediate_image_sizes_advanced', $sizes);
        foreach ($sizes as $size => $size_data) {
            $resized = image_make_intermediate_size($file, $size_data['width'], $size_data['height'], $size_data['crop']);
            if ($resized) {
                $metadata['sizes'][$size] = $resized;
            }
        }
        // fetch additional metadata from exif/iptc
        $image_meta = wp_read_image_metadata($file);
        if ($image_meta) {
            $metadata['image_meta'] = $image_meta;
        }
    }
    return apply_filters('wp_generate_attachment_metadata', $metadata, $attachment_id);
}
开发者ID:voodoobettie,项目名称:staypressproperty,代码行数:56,代码来源:functions.php

示例6: test_make_intermediate_size_successful

 function test_make_intermediate_size_successful()
 {
     if (!function_exists('imagejpeg')) {
         $this->markTestSkipped('jpeg support unavailable');
     }
     $image = image_make_intermediate_size(DIR_TESTDATA . '/images/a2-small.jpg', 100, 75, true);
     $this->assertInternalType('array', $image);
     $this->assertEquals(100, $image['width']);
     $this->assertEquals(75, $image['height']);
     $this->assertEquals('image/jpeg', $image['mime-type']);
     $this->assertFalse(isset($image['path']));
     unlink(DIR_TESTDATA . '/images/a2-small-100x75.jpg');
 }
开发者ID:boonebgorges,项目名称:wp,代码行数:13,代码来源:intermediate_size.php

示例7: module_file_upload

 function module_file_upload()
 {
     // name of module
     $module = mgm_request_var('module', '', true);
     // file
     $file_element = 'logo_' . $module;
     // init
     $logo = array();
     // init messages
     $status = 'error';
     $message = __('Logo upload failed.', 'mgm');
     // upload check
     if (is_uploaded_file($_FILES[$file_element]['tmp_name'])) {
         // random filename
         $uniquename = substr(microtime(), 2, 8);
         // paths
         $oldname = strtolower($_FILES[$file_element]['name']);
         $newname = preg_replace('/(.*)\\.(png|jpg|jpeg|gif)$/i', $uniquename . '.$2', $oldname);
         $filepath = MGM_FILES_MODULE_DIR . $newname;
         // upload
         if (move_uploaded_file($_FILES[$file_element]['tmp_name'], $filepath)) {
             // get thumb
             $thumb = image_make_intermediate_size(MGM_FILES_MODULE_DIR . $newname, 100, 100);
             // set logo
             if ($thumb) {
                 $logo = array('image_name' => $thumb['file'], 'image_url' => MGM_FILES_MODULE_URL . $thumb['file']);
                 // remove main file, we dont need it
                 mgm_delete_file($filepath);
             } else {
                 $logo = array('image_name' => $newname, 'image_url' => MGM_FILES_MODULE_URL . $newname);
             }
             // status
             $status = 'success';
             $message = __('logo uploaded successfully, it will be attached when you update the settings.', 'mgm');
         }
     }
     // send ouput
     @ob_end_clean();
     // PRINT
     echo json_encode(array('status' => $status, 'message' => $message, 'logo' => $logo));
     // end out put
     @ob_flush();
     exit;
 }
开发者ID:jervy-ez,项目名称:magic-members-test,代码行数:44,代码来源:mgm_admin_payments.php

示例8: make_intermediate_image_size

 private function make_intermediate_image_size($source, $filename, $dest_dir, $width, $height, $crop = false, $suffix = '')
 {
     $pathinfo = awpcp_utf8_pathinfo($source);
     $safe_suffix = empty($suffix) ? '.' : "-{$suffix}.";
     $extension = $pathinfo['extension'];
     $parent_directory = $pathinfo['dirname'];
     $generated_image_name = $filename . $safe_suffix . $extension;
     $generated_image_path = implode(DIRECTORY_SEPARATOR, array($dest_dir, $generated_image_name));
     $generated_image = image_make_intermediate_size($source, $width, $height, $crop);
     if (is_array($generated_image)) {
         $temporary_image_path = implode(DIRECTORY_SEPARATOR, array($parent_directory, $generated_image['file']));
         $result = rename($temporary_image_path, $generated_image_path);
     }
     if (!isset($result) || $result === false) {
         $result = copy($source, $generated_image_path);
     }
     chmod($generated_image_path, 0644);
     return $result;
 }
开发者ID:sabdev1,项目名称:ljcdevsab,代码行数:19,代码来源:class-image-resizer.php

示例9: make_intermediate_image_size

 private function make_intermediate_image_size($file, $destination_dir, $width, $height, $crop = false, $suffix = '')
 {
     if (!file_exists($destination_dir) && !mkdir($destination_dir, awpcp_directory_permissions(), true)) {
         throw new AWPCP_Exception(__("Destination directory doesn't exists and couldn't be created.", 'AWPCP'));
     }
     $image = image_make_intermediate_size($file->get_path(), $width, $height, $crop);
     $safe_suffix = empty($suffix) ? '.' : "-{$suffix}.";
     $image_name = $file->get_file_name() . $safe_suffix . $file->get_extension();
     $image_path = implode(DIRECTORY_SEPARATOR, array($destination_dir, $image_name));
     if (is_array($image)) {
         $source_path = implode(DIRECTORY_SEPARATOR, array($file->get_parent_directory(), $image['file']));
         $result = rename($source_path, $image_path);
     }
     if (!isset($result) || $result === false) {
         $result = copy($file->get_path(), $image_path);
     }
     chmod($image_path, 0644);
     return $result;
 }
开发者ID:sabdev1,项目名称:ljcdevsab,代码行数:19,代码来源:class-image-file-processor.php

示例10: create_image_sizes

 /**
  * Create Image Sizes
  *
  * @since 1.1.2
  */
 function create_image_sizes($filepath)
 {
     $sizes = array();
     foreach (get_intermediate_image_sizes() as $s) {
         $sizes[$s] = array('width' => '', 'height' => '', 'crop' => true);
         $sizes[$s]['width'] = get_option("{$s}_size_w");
         // For default sizes set in options
         $sizes[$s]['height'] = get_option("{$s}_size_h");
         // For default sizes set in options
         $sizes[$s]['crop'] = get_option("{$s}_crop");
         // For default sizes set in options
     }
     // foreach()
     $sizes = apply_filters('intermediate_image_sizes_advanced', $sizes);
     $metadata = array();
     foreach ($sizes as $size => $size_data) {
         $resized = image_make_intermediate_size($filepath, $size_data['width'], $size_data['height'], $size_data['crop']);
         if ($resized) {
             $metadata[$size] = $resized;
         }
     }
     // foreach()
     return $metadata;
 }
开发者ID:narendra-addweb,项目名称:MyImmoPix,代码行数:29,代码来源:class-wpmfu-file-upload-handler.php

示例11: generate_featured_size

 function generate_featured_size($media_id)
 {
     $metadata = wp_get_attachment_metadata($media_id);
     $resized = image_make_intermediate_size(get_attached_file($media_id), $this->settings['width'], $this->settings['height'], $this->settings['crop']);
     if ($resized) {
         $metadata['sizes']['rt_media_featured_image'] = $resized;
         wp_update_attachment_metadata($media_id, $metadata);
     }
 }
开发者ID:fs-contributor,项目名称:rtMedia,代码行数:9,代码来源:RTMediaFeatured.php

示例12: gambit_otf_regen_thumbs_media_downsize

 /**
  * The downsizer. This only does something if the existing image size doesn't exist yet.
  *
  * @param	$out boolean false
  * @param	$id int Attachment ID
  * @param	$size mixed The size name, or an array containing the width & height
  * @return	mixed False if the custom downsize failed, or an array of the image if successful
  */
 function gambit_otf_regen_thumbs_media_downsize($out, $id, $size)
 {
     // Gather all the different image sizes of WP (thumbnail, medium, large) and,
     // all the theme/plugin-introduced sizes.
     global $_gambit_otf_regen_thumbs_all_image_sizes;
     if (!isset($_gambit_otf_regen_thumbs_all_image_sizes)) {
         global $_wp_additional_image_sizes;
         $_gambit_otf_regen_thumbs_all_image_sizes = array();
         $interimSizes = get_intermediate_image_sizes();
         foreach ($interimSizes as $sizeName) {
             if (in_array($sizeName, array('thumbnail', 'medium', 'large'))) {
                 $_gambit_otf_regen_thumbs_all_image_sizes[$sizeName]['width'] = get_option($sizeName . '_size_w');
                 $_gambit_otf_regen_thumbs_all_image_sizes[$sizeName]['height'] = get_option($sizeName . '_size_h');
                 $_gambit_otf_regen_thumbs_all_image_sizes[$sizeName]['crop'] = (bool) get_option($sizeName . '_crop');
             } elseif (isset($_wp_additional_image_sizes[$sizeName])) {
                 $_gambit_otf_regen_thumbs_all_image_sizes[$sizeName] = $_wp_additional_image_sizes[$sizeName];
             }
         }
     }
     // This now contains all the data that we have for all the image sizes
     $allSizes = $_gambit_otf_regen_thumbs_all_image_sizes;
     // If image size exists let WP serve it like normally
     $imagedata = wp_get_attachment_metadata($id);
     // Image attachment doesn't exist
     if (!is_array($imagedata)) {
         return false;
     }
     // If the size given is a string / a name of a size
     if (is_string($size)) {
         // If WP doesn't know about the image size name, then we can't really do any resizing of our own
         if (empty($allSizes[$size])) {
             return false;
         }
         // If the size has already been previously created, use it
         if (!empty($imagedata['sizes'][$size]) && !empty($allSizes[$size])) {
             // But only if the size remained the same
             if ($allSizes[$size]['width'] == $imagedata['sizes'][$size]['width'] && $allSizes[$size]['height'] == $imagedata['sizes'][$size]['height']) {
                 return false;
             }
             // Or if the size is different and we found out before that the size really was different
             if (!empty($imagedata['sizes'][$size]['width_query']) && !empty($imagedata['sizes'][$size]['height_query'])) {
                 if ($imagedata['sizes'][$size]['width_query'] == $allSizes[$size]['width'] && $imagedata['sizes'][$size]['height_query'] == $allSizes[$size]['height']) {
                     return false;
                 }
             }
         }
         // Resize the image
         $resized = image_make_intermediate_size(get_attached_file($id), $allSizes[$size]['width'], $allSizes[$size]['height'], $allSizes[$size]['crop']);
         // Resize somehow failed
         if (!$resized) {
             return false;
         }
         // Save the new size in WP
         $imagedata['sizes'][$size] = $resized;
         // Save some additional info so that we'll know next time whether we've resized this before
         $imagedata['sizes'][$size]['width_query'] = $allSizes[$size]['width'];
         $imagedata['sizes'][$size]['height_query'] = $allSizes[$size]['height'];
         wp_update_attachment_metadata($id, $imagedata);
         // Serve the resized image
         $att_url = wp_get_attachment_url($id);
         return array(dirname($att_url) . '/' . $resized['file'], $resized['width'], $resized['height'], true);
         // If the size given is a custom array size
     } else {
         if (is_array($size)) {
             $imagePath = get_attached_file($id);
             // This would be the path of our resized image if the dimensions existed
             $imageExt = pathinfo($imagePath, PATHINFO_EXTENSION);
             $imagePath = preg_replace('/^(.*)\\.' . $imageExt . '$/', sprintf('$1-%sx%s.%s', $size[0], $size[1], $imageExt), $imagePath);
             $att_url = wp_get_attachment_url($id);
             // If it already exists, serve it
             if (file_exists($imagePath)) {
                 return array(dirname($att_url) . '/' . basename($imagePath), $size[0], $size[1], true);
             }
             // If not, resize the image...
             $resized = image_make_intermediate_size(get_attached_file($id), $size[0], $size[1], true);
             // Resize somehow failed
             if (!$resized) {
                 return false;
             }
             // Then serve it
             return array(dirname($att_url) . '/' . $resized['file'], $resized['width'], $resized['height'], true);
         }
     }
     return false;
 }
开发者ID:kadr,项目名称:semashko,代码行数:93,代码来源:otf_regen_thumbs.php

示例13: wp_generate_attachment_metadata_custom

 /**
  * Generate post thumbnail attachment meta data.
  *
  * @since 2.1.0
  *
  * @param int $attachment_id Attachment Id to process.
  * @param string $file Filepath of the Attached image.
  *
  * @param null|array $thumbnails: thumbnails to regenerate, if null all
  *
  * @return mixed Metadata for attachment.
  */
 public static function wp_generate_attachment_metadata_custom($attachment_id, $file, $thumbnails = null)
 {
     $attachment = get_post($attachment_id);
     $meta_datas = get_post_meta($attachment_id, '_wp_attachment_metadata', true);
     $metadata = array();
     if (preg_match('!^image/!', get_post_mime_type($attachment)) && file_is_displayable_image($file)) {
         $imagesize = getimagesize($file);
         $metadata['width'] = $imagesize[0];
         $metadata['height'] = $imagesize[1];
         list($uwidth, $uheight) = wp_constrain_dimensions($metadata['width'], $metadata['height'], 128, 96);
         $metadata['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
         // Make the file path relative to the upload dir
         $metadata['file'] = _wp_relative_upload_path($file);
         // make thumbnails and other intermediate sizes
         global $_wp_additional_image_sizes;
         foreach (get_intermediate_image_sizes() as $s) {
             $sizes[$s] = array('width' => '', 'height' => '', 'crop' => false);
             if (isset($_wp_additional_image_sizes[$s]['width'])) {
                 $sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']);
             } else {
                 $sizes[$s]['width'] = get_option("{$s}_size_w");
             }
             // For default sizes set in options
             if (isset($_wp_additional_image_sizes[$s]['height'])) {
                 $sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']);
             } else {
                 $sizes[$s]['height'] = get_option("{$s}_size_h");
             }
             // For default sizes set in options
             if (isset($_wp_additional_image_sizes[$s]['crop'])) {
                 $sizes[$s]['crop'] = intval($_wp_additional_image_sizes[$s]['crop']);
             } else {
                 $sizes[$s]['crop'] = get_option("{$s}_crop");
             }
             // For default sizes set in options
         }
         $sizes = apply_filters('intermediate_image_sizes_advanced', $sizes);
         // Only if not all sizes
         if (isset($thumbnails) && is_array($thumbnails) && isset($meta_datas['sizes']) && !empty($meta_datas['sizes'])) {
             // Fill the array with the other sizes not have to be done
             foreach ($meta_datas['sizes'] as $name => $fsize) {
                 $metadata['sizes'][$name] = $fsize;
             }
         }
         foreach ($sizes as $size => $size_data) {
             if (isset($thumbnails)) {
                 if (!in_array($size, $thumbnails)) {
                     continue;
                 }
             }
             $resized = image_make_intermediate_size($file, $size_data['width'], $size_data['height'], $size_data['crop']);
             if (isset($meta_datas['size'][$size])) {
                 // Remove the size from the orignal sizes for after work
                 unset($meta_datas['size'][$size]);
             }
             if ($resized) {
                 $metadata['sizes'][$size] = $resized;
             }
         }
         // fetch additional metadata from exif/iptc
         $image_meta = wp_read_image_metadata($file);
         if ($image_meta) {
             $metadata['image_meta'] = $image_meta;
         }
     }
     return apply_filters('wp_generate_attachment_metadata', $metadata, $attachment_id);
 }
开发者ID:waynestedman,项目名称:commodore-new,代码行数:79,代码来源:main.php

示例14: get_attachment_image_src

 public function get_attachment_image_src($pid, $size_name = 'thumbnail', $check_dupes = true, $force_regen = false)
 {
     if ($this->p->debug->enabled) {
         $this->p->debug->args(array('pid' => $pid, 'size_name' => $size_name, 'check_dupes' => $check_dupes, 'force_regen' => $force_regen));
     }
     $size_info = $this->get_size_info($size_name);
     $img_url = '';
     $img_width = -1;
     $img_height = -1;
     $img_cropped = $size_info['crop'] === false ? 0 : 1;
     // get_size_info() returns false, true, or an array
     $ret_empty = array(null, null, null, null, null);
     if ($this->p->is_avail['media']['ngg'] === true && strpos($pid, 'ngg-') === 0) {
         if (!empty($this->p->mods['media']['ngg'])) {
             return $this->p->mods['media']['ngg']->get_image_src($pid, $size_name, $check_dupes);
         } else {
             if ($this->p->debug->enabled) {
                 $this->p->debug->log('ngg module is not available: image ID ' . $attr_value . ' ignored');
             }
             if (is_admin()) {
                 $this->p->notice->err('The NextGEN Gallery module is not available: image ID ' . $pid . ' ignored.');
             }
             return $ret_empty;
         }
     } elseif (!wp_attachment_is_image($pid)) {
         if ($this->p->debug->enabled) {
             $this->p->debug->log('exiting early: attachment ' . $pid . ' is not an image');
         }
         return $ret_empty;
     }
     if (strpos($size_name, $this->p->cf['lca'] . '-') !== false) {
         // only resize our own custom image sizes
         if (!empty($this->p->options['plugin_auto_img_resize'])) {
             // auto-resize images option must be enabled
             $img_meta = wp_get_attachment_metadata($pid);
             // does the image metadata contain our image sizes?
             if ($force_regen === true || empty($img_meta['sizes'][$size_name])) {
                 $is_accurate_width = false;
                 $is_accurate_height = false;
             } else {
                 // is the width and height in the image metadata accurate?
                 $is_accurate_width = !empty($img_meta['sizes'][$size_name]['width']) && $img_meta['sizes'][$size_name]['width'] == $size_info['width'] ? true : false;
                 $is_accurate_height = !empty($img_meta['sizes'][$size_name]['height']) && $img_meta['sizes'][$size_name]['height'] == $size_info['height'] ? true : false;
                 // if not cropped, make sure the resized image respects the original aspect ratio
                 if ($is_accurate_width && $is_accurate_height && $img_cropped === 0) {
                     if ($img_meta['width'] > $img_meta['height']) {
                         $ratio = $img_meta['width'] / $size_info['width'];
                         $check = 'height';
                     } else {
                         $ratio = $img_meta['height'] / $size_info['height'];
                         $check = 'width';
                     }
                     $should_be = (int) round($img_meta[$check] / $ratio);
                     // allow for a +/-1 pixel difference
                     if ($img_meta['sizes'][$size_name][$check] < $should_be - 1 || $img_meta['sizes'][$size_name][$check] > $should_be + 1) {
                         $is_accurate_width = false;
                         $is_accurate_height = false;
                     }
                 }
             }
             // depending on cropping, one or both sides of the image must be accurate
             // if not, attempt to create a resized image by calling image_make_intermediate_size()
             if (empty($size_info['crop']) && (!$is_accurate_width && !$is_accurate_height) || !empty($size_info['crop']) && (!$is_accurate_width || !$is_accurate_height)) {
                 if ($this->p->debug->enabled) {
                     if (empty($img_meta['sizes'][$size_name])) {
                         $this->p->debug->log($size_name . ' size not defined in the image meta');
                     } else {
                         $this->p->debug->log('image metadata (' . (empty($img_meta['sizes'][$size_name]['width']) ? 0 : $img_meta['sizes'][$size_name]['width']) . 'x' . (empty($img_meta['sizes'][$size_name]['height']) ? 0 : $img_meta['sizes'][$size_name]['height']) . ') does not match ' . $size_name . ' (' . $size_info['width'] . 'x' . $size_info['height'] . ($img_cropped === 0 ? '' : ' cropped') . ')');
                     }
                 }
                 $fullsizepath = get_attached_file($pid);
                 $resized = image_make_intermediate_size($fullsizepath, $size_info['width'], $size_info['height'], $size_info['crop']);
                 if ($this->p->debug->enabled) {
                     $this->p->debug->log('image_make_intermediate_size() reported ' . ($resized === false ? 'failure' : 'success'));
                 }
                 if ($resized !== false) {
                     $img_meta['sizes'][$size_name] = $resized;
                     wp_update_attachment_metadata($pid, $img_meta);
                 }
             }
         } elseif ($this->p->debug->enabled) {
             $this->p->debug->log('image metadata check skipped: plugin_auto_img_resize option is disabled');
         }
     }
     list($img_url, $img_width, $img_height) = apply_filters($this->p->cf['lca'] . '_image_downsize', image_downsize($pid, $size_name), $pid, $size_name);
     if ($this->p->debug->enabled) {
         $this->p->debug->log('image_downsize() = ' . $img_url . ' (' . $img_width . 'x' . $img_height . ')');
     }
     if (empty($img_url)) {
         if ($this->p->debug->enabled) {
             $this->p->debug->log('exiting early: returned image_downsize() url is empty');
         }
         return $ret_empty;
     }
     // check for resulting image dimensions that may be too small
     if (!empty($this->p->options['plugin_ignore_small_img'])) {
         $is_sufficient_width = $img_width >= $size_info['width'] ? true : false;
         $is_sufficient_height = $img_height >= $size_info['height'] ? true : false;
         if ($img_width > 0 && $img_height > 0) {
             // just in case
//.........这里部分代码省略.........
开发者ID:jamesvillarrubia,项目名称:uniken-web,代码行数:101,代码来源:media.php

示例15: thumbIt

 /**
  * Takes an image URL (or attachment ID) and returns a URL to a version of that same image that is equal in dimensions to the passed $width and $height parameters
  * The image will be resized on-the-fly, saved, and returned if an image of that same size doesn't already exist in the media library
  *
  * @param string|int $image The image URL or attachment ID whose resized version the function should return
  * @param int $width The desired width of the returned image
  * @param int $height The desired height of the returned image
  * @param boolean $crop Should the image be cropped to the desired dimensions (Defaults to false in which case the image is scaled down, rather than cropped)
  * @return string
  */
 public static function thumbIt($image, $width, $height, $crop = true)
 {
     global $wpdb;
     if (is_int($image)) {
         $attachment_id = $image > 0 ? $image : false;
     } else {
         $img_url = esc_url($image);
         $upload_dir = wp_upload_dir();
         $base_url = $upload_dir['baseurl'];
         if (substr($img_url, 0, strlen($base_url)) !== $base_url) {
             return $image;
         }
         $result = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s LIMIT 1;", '%' . wpdb::esc_like(str_replace(trailingslashit($base_url), '', $img_url)) . '%'));
         $attachment_id = absint($result) > 0 ? absint($result) : false;
     }
     if ($attachment_id === false) {
         return $image;
     }
     $image = wp_get_attachment_url($attachment_id);
     $attachment_meta = wp_get_attachment_metadata($attachment_id);
     if ($attachment_meta === false) {
         return $image;
     }
     $width = absint($width);
     $height = absint($height);
     $needs_resize = true;
     foreach ($attachment_meta['sizes'] as $size) {
         if ($width === $size['width'] && $height === $size['height']) {
             $image = str_replace(basename($image), $size['file'], $image);
             $needs_resize = false;
             break;
         }
     }
     if ($needs_resize) {
         $attached_file = get_attached_file($attachment_id);
         $resized = image_make_intermediate_size($attached_file, $width, $height, (bool) $crop);
         if (!is_wp_error($resized) && $resized !== false) {
             $key = sprintf('resized-%dx%d', $width, $height);
             $attachment_meta['sizes'][$key] = $resized;
             $image = str_replace(basename($image), $resized['file'], $image);
             wp_update_attachment_metadata($attachment_id, $attachment_meta);
             $backup_sizes = get_post_meta($attachment_id, '_wp_attachment_backup_sizes', true);
             if (!is_array($backup_sizes)) {
                 $backup_sizes = array();
             }
             $backup_sizes[$key] = $resized;
             update_post_meta($attachment_id, '_wp_attachment_backup_sizes', $backup_sizes);
         }
     }
     return $image;
 }
开发者ID:MBerguer,项目名称:wp-demo,代码行数:61,代码来源:ui.php


注:本文中的image_make_intermediate_size函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。