wordpress从顶部开始裁剪图片的方法

wordpress认情况下,裁剪的图片会直接裁剪图片的中间部分,例如你上传了一张美女图片上传上去由于图片的尺寸超出了内部规定的尺寸,wordpress会对其进行裁剪,按照规定的尺寸裁剪好后生成另外一张图片进行保存,可惜的是,当你调用这张图片的时候发现,美女的头被卡擦了,这样的缩略图完全失去了吸引读者的功效,下面我们就来解决这个问题

PHPcode4"> absint( $width ),'height' => absint( $height ),'crop' => $crop );} /** * Returning no sizes (an empty array) will force * wp_generate_attachment_Metadata to skip creating intermediate image sizes on * upload,then we can run our own resizing functions by hooking into the * 'wp_generate_attachment_Metadata' filter */function bt_intermediate_image_sizes_advanced( $sizes ) { return array();} function bt_generate_attachment_Metadata( $Metadata,$attachment_id ) { $attachment = get_post( $attachment_id ); $uploadpath = wp_upload_dir(); $file = path_join($uploadpath['basedir'],$Metadata['file']); if ( !preg_match('!^image/!',get_post_mime_type( $attachment )) || !file_is_displayable_image( $file ) ) return $Metadata; 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'] ); // For theme-added sizes 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'] ); // For theme-added sizes 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'] = $_wp_additional_image_sizes[$s]['crop']; else $sizes[$s]['crop'] = get_option( "{$s}_crop" ); } foreach ( $sizes as $size => $size_data ) { $resized = bt_image_make_intermediate_size( $file,$size_data['width'],$size_data['height'],$size_data['crop'] ); if ( $resized ) $Metadata['sizes'][$size] = $resized; } return $Metadata;} /** * Resize an image to make a thumbnail or intermediate size. * * The returned array has the file size,the image width,and image height. The * filter 'image_make_intermediate_size' can be used to hook in and change the * values of the returned array. The only parameter is the resized file path. * * @param string $file File path. * @param int $width Image width. * @param int $height Image height. * @param bool|array $crop Optional,if no image was created. Metadata array on success. */function bt_image_make_intermediate_size( $file,$width,$height,$crop = false ) { if ( $width || $height ) { $resized_file = bt_image_resize( $file,$crop,null,90 ); if ( !is_wp_error( $resized_file ) && $resized_file && $info = getimagesize( $resized_file ) ) { $resized_file = apply_filters('image_make_intermediate_size',$resized_file); return array( 'file' => wp_basename( $resized_file ), 'width' => $info[0], 'height' => $info[1], ); } } return false;} /** * Retrieve calculated resized dimensions for use in imagecopyresampled(). * * Calculate dimensions and coordinates for a resized image that fits within a * specified width and height. If $crop is true,the largest matching central * portion of the image will be cropped out and resized to the required size. * * @param int $orig_w Original width. * @param int $orig_h Original height. * @param int $dest_w New width. * @param int $dest_h New height. * @param bool $crop Optional,default is false. Whether to crop image or resize. * @return bool|array False,on failure. Returned array matches parameters for imagecopyresampled() PHP function. */function bt_image_resize_dimensions($orig_w,$orig_h,$dest_w,$dest_h,$crop = false) { if ($orig_w <= 0 || $orig_h <= 0) return false; // at least one of dest_w or dest_h must be specific if ($dest_w <= 0 && $dest_h <= 0) return false; if ( $crop ) { // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h $aspect_ratio = $orig_w / $orig_h; $new_w = min($dest_w,$orig_w); $new_h = min($dest_h,$orig_h); if ( !$new_w ) { $new_w = intval($new_h * $aspect_ratio); } if ( !$new_h ) { $new_h = intval($new_w / $aspect_ratio); } $size_ratio = max($new_w / $orig_w,$new_h / $orig_h); $crop_w = round($new_w / $size_ratio); $crop_h = round($new_h / $size_ratio); if ( !is_array( $crop ) || count( $crop ) != 2 ) { $crop = apply_filters( 'image_resize_crop_default','center' ) ); } switch ( $crop[0] ) { case 'left': $s_x = 0; break; case 'right': $s_x = $orig_w - $crop_w; break; default: $s_x = floor( ( $orig_w - $crop_w ) / 2 ); } switch ( $crop[1] ) { case 'top': $s_y = 0; break; case 'bottom': $s_y = $orig_h - $crop_h; break; default: $s_y = floor( ( $orig_h - $crop_h ) / 2 ); } } else { // don't crop,just resize using $dest_w x $dest_h as a maximum bounding box $crop_w = $orig_w; $crop_h = $orig_h; $s_x = 0; $s_y = 0; list( $new_w,$new_h ) = wp_constrain_dimensions( $orig_w,$dest_h ); } // if the resulting image would be the same size or larger we don't want to resize it if ( $new_w >= $orig_w && $new_h >= $orig_h ) return false; // the return array matches the parameters to imagecopyresampled() // int dst_x,int dst_y,int src_x,int src_y,int dst_w,int dst_h,int src_w,int src_h return array( 0,(int) $s_x,(int) $s_y,(int) $new_w,(int) $new_h,(int) $crop_w,(int) $crop_h ); } /** * Scale down an image to fit a particular size and save a new copy of the image. * * The PNG transparency will be preserved using the function,as well as the * image type. If the file going in is PNG,then the resized image is going to * be PNG. The only supported image types are PNG,GIF,and JPEG. * * Some functionality requires API to exist,so some PHP version may lose out * support. This is not the fault of wordpress (where functionality is * downgraded,not actual defects),but of your PHP version. * * @since 2.5.0 * * @param string $file Image file path. * @param int $max_w Maximum width to resize to. * @param int $max_h Maximum height to resize to. * @param bool $crop Optional. Whether to crop image or resize. * @param string $suffix Optional. File Suffix. * @param string $dest_path Optional. New image file path. * @param int $jpeg_quality Optional,default is 90. Image quality percentage. * @return mixed WP_Error on failure. String with new destination path. */function bt_image_resize( $file,$max_w,$max_h,$crop = false,$suffix = null,$dest_path = null,$jpeg_quality = 90 ) { $image = wp_load_image( $file ); if ( !is_resource( $image ) ) return new WP_Error( 'error_loading_image',$image,$file ); $size = @getimagesize( $file ); if ( !$size ) return new WP_Error('invalid_image',__('Could not read image size'),$file); list($orig_w,$orig_type) = $size; // Rotate if EXIF 'Orientation' is set // This code is from the reverted patch at // wordpress.org/changeset/11746/trunk/wp-includes/media.PHP">http://core.trac.wordpress.org/changeset/11746/trunk/wp-includes/media.PHP $rotate = false; if ( is_callable( 'exif_read_data' ) && in_array( $orig_type,apply_filters( 'wp_read_image_Metadata_types',array( IMAGETYPE_JPEG,IMAGETYPE_TIFF_II,IMAGETYPE_TIFF_MM ) ) ) ) { $exif = @exif_read_data( $file,true ); if ( $exif && isset( $exif['IFD0'] ) && is_array( $exif['IFD0'] ) && isset( $exif['IFD0']['Orientation'] ) ) { if ( 6 == $exif['IFD0']['Orientation'] ) $rotate = 90; elseif ( 8 == $exif['IFD0']['Orientation'] ) $rotate = 270; } } if ( $rotate ) list($max_h,$max_w) = array($max_w,$max_h); $dims = bt_image_resize_dimensions($orig_w,$crop); if ( !$dims ) return new WP_Error( 'error_getting_dimensions',__('Could not calculate resized image dimensions') ); list($dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h) = $dims; $newimage = wp_imagecreatetruecolor( $dst_w,$dst_h ); if ( $rotate ) list($src_y,$src_x) = array($src_x,$src_y); imagecopyresampled( $newimage,$dst_x,$src_h); // convert from full colors to index colors,like original PNG. if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $image ) ) imagetruecolortopalette( $newimage,false,imagecolorstotal( $image ) ); // we don't need the original in memory anymore imagedestroy( $image ); // $suffix will be appended to the destination filename,just before the extension if ( !$suffix ) { if ( $rotate ) $suffix = "{$dst_h}x{$dst_w}"; else $suffix = "{$dst_w}x{$dst_h}"; } $info = pathinfo($file); $dir = $info['dirname']; $ext = $info['extension']; $name = wp_basename($file,".$ext"); if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) ) $dir = $_dest_path; $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}"; if ( IMAGETYPE_GIF == $orig_type ) { if ( !imagegif( $newimage,$destfilename ) ) return new WP_Error('resize_path_invalid',__( 'Resize path invalid' )); } elseif ( IMAGETYPE_PNG == $orig_type ) { if ( !imagepng( $newimage,__( 'Resize path invalid' )); } else { if ( $rotate ) { $newimage = _rotate_image_resource( $newimage,360 - $rotate ); } // all other formats are converted to jpg $destfilename = "{$dir}/{$name}-{$suffix}.jpg"; $return = imagejpeg( $newimage,$destfilename,apply_filters( 'jpeg_quality',$jpeg_quality,'image_resize' ) ); if ( !$return ) return new WP_Error('resize_path_invalid',__( 'Resize path invalid' )); } imagedestroy( $newimage ); // Set correct file permissions $stat = stat( dirname( $destfilename )); $perms = $stat['mode'] & 0000666; //same permissions as parent folder,strip off the executable bits @ chmod( $destfilename,$perms ); return $destfilename;}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


今天看到群友突然滴滴滴的找我QQ,好像出什么大事情一样。原来他由于误操作导致网站文章被删除。
今天遇到一个网友提示网站访问速度比较慢,然后通知日志看到有他的网站WordPress默认登录地址wp-login.php一直在被爬虫抓取,因为是有被破解后台账户信息,导致的占用数据库资源使得网站资源带宽不足后访问降低导致
今天老左在帮助客户升级WordPress的时候我看他网站也比较简单就直接在后台手工升级的。升级后打开网站有出现"There has been a critical error on your website."的错误提示。
我们在使用WordPress搭建的网站是否有留意查看源代码的时候看到标题中的原本"-",被转义成"&#8211"。其实这样并不影响用户阅读体验和搜索引擎,搜索引擎在收录后也会反向转义的。就是感觉看
WordPress默认是无法上传.webp格式的文件,如果要上传需要对上传文件限制进行解除后才能上传。
GTranslate 是一个流行的翻译插件,在免费和付费版本中提供不同的体验。 使用免费版本,您可以轻松提供任何语言的动态翻译,而付费版本提供可索引、可编辑的翻译版本,就像 Weglot 一样。
时下主流的浏览器都自带了广告屏蔽功能,还有ADSafe、ADB等软件插件,不断更新,屏蔽网站代码!
您是否正在寻找可用于显示数据的 WordPress 表格插件? WordPress 表格插件可让您以表格格式存储和呈现数据,以方便访问者阅读。
wordPress数据库除了本身的表以外,部分插件会增加表来存储数据。如果插件已经删除了,这些插件生成的表还在数据库中,那么怎么删除呢?
WordPress几乎是世界上最安全的开源程序,但其后台登录注册的地址始终是众所周知的,所以不少人觉得隐藏WordPress的登录地址后可能会更加安全。下面浩子就针对这个问题来简单说明一下方法。WordPress隐藏后台登录地址
自 WordPress 5.8 正式版发布 以后,后台的小工具就默认是块编辑器的界面样式,很多老用户还没有来得及学习就一脸茫然了,今天浩子就来教大家如何恢复WordPress经典小工具。
您想用您的语言翻译 WordPress 插件吗?许多 WordPress 插件是完全可翻译的,世界上任何人都可以轻松翻译。在本文中,我们将向您展示如何轻松地将 WordPress 插件翻译成您的语言(无需任何代码)。
WordPress新手不清楚WordPress调试模式怎么开启,今天来教大家认识和开启。什么是WordPress的调试模式
最近不少用户反馈说WordPress后台的外观-自定义点开后直接报错,错误信息如下:
前段时间迁移网站的时候,需要导入 sql 文件,小编用的是phpmyadmin管理工具,无论是从本地上传.sql.zip 文件,还是从网站服务器上文件夹 upload 下选择 sql 文件,都上传失败,皆以 502 Bad Gateway 告终。因为时间
WordPress程序报错的问题报错代码如下:This page contains the following errors:error on line 1 at column 6: XML declaration allowed only at the start of the documentBelow is a rendering of the page up t
DedeCMS织梦系统是国内的优秀开源建站系统,使用的网站数量众多,不过近期织梦也要开始收取授权费用了:除个人非盈利网站外,均需要购买DedeCMS商业使用授权,授权费为5800元。
WordPress生成XML站点地图的方法有很多,归纳总结一下,大致可以通过WordPress插件、在线生成软件或网站、PHP代码等三种方法实现,各有利弊,就看WordPress开发者怎么选择。
开发主题时使用了wordpress的缓存函数,如下所示代码,但是在打印缓存时,怎么显示的都是false,经过查找才了解到。默认情况下,对象缓存是非持久的。这意味着存储在缓存中的数据仅在请求期间驻留在内存中。除非您安装持久缓存插件,否则缓存数据不会在页面加载时持久存储。就这个意思,想持久就需要找个插件装上。function get_term_post($term_id){if($term_id &l
在 WordPress 主题和插件开发中,条件判断标签(Conditional Tags)是非常重要的,通过条件判断标签,我们可以判断各种情况,从而使用对应的代码等。例如在顶部导航条中有一个“首页”选项,在首页的时候需要设置成高亮,我们可以在头部导航模板文件(header.php)中使用条件判断标签 is_home() 来判断当前页面是不是首页,然后再输出高亮的 class。这类的条件判断标签一般