使用PHP创建缩略图。 (裁剪到正方形) [英] Use PHP to create thumbnails. (Cropped to square)

查看:303
本文介绍了使用PHP创建缩略图。 (裁剪到正方形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用的PHP脚本根据最大宽度和高度创建缩略图。但是,我希望它能够始终创建方形图像并在需要时裁剪图像。

I have a php script im currently using that creates thumbnails based on a max width and height. However, I'd like it to always create square images and crop the images when needed.

以下是我现在使用的内容:

Here is what I'm using now:

    function makeThumb( $filename, $type ) {
  global $max_width, $max_height;
  if ( $type == 'jpg' ) {
   $src = imagecreatefromjpeg("blocks/img/gallery/" . $filename);
  } else if ( $type == 'png' ) {
   $src = imagecreatefrompng("blocks/img/gallery/" . $filename);
  } else if ( $type == 'gif' ) {
   $src = imagecreatefromgif("blocks/img/gallery/" . $filename);
  }
  if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
   $newW = $oldW * ($max_width / $oldH);
   $newH = $max_height;
  } else {
   $newW = $max_width;
   $newH = $oldH * ($max_height / $oldW);
  }
  $new = imagecreatetruecolor($newW, $newH);
  imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
  if ( $type == 'jpg' ) {
   imagejpeg($new, 'blocks/img/gallery/thumbs/'.$filename);
  } else if ( $type == 'png' ) {
   imagepng($new, 'blocks/img/gallery/thumbs/'.$filename);
  } else if ( $type == 'gif' ) {
   imagegif($new, 'blocks/img/gallery/thumbs/'.$filename);
  }
  imagedestroy($new);
  imagedestroy($src);
 }

我如何改变这个来完成我想要的东西(方形拇指)?

How would I alter this to accomplish what I want (Square thumbs)?

提前致谢。

推荐答案

function makeThumb( $filename , $thumbSize=100 ){
  global $max_width, $max_height;
 /* Set Filenames */
  $srcFile = 'blocks/img/gallery/'.$filename;
  $thumbFile = 'blocks/img/gallery/thumbs/'.$filename;
 /* Determine the File Type */
  $type = substr( $filename , strrpos( $filename , '.' )+1 );
 /* Create the Source Image */
  switch( $type ){
    case 'jpg' : case 'jpeg' :
      $src = imagecreatefromjpeg( $srcFile ); break;
    case 'png' :
      $src = imagecreatefrompng( $srcFile ); break;
    case 'gif' :
      $src = imagecreatefromgif( $srcFile ); break;
  }
 /* Determine the Image Dimensions */
  $oldW = imagesx( $src );
  $oldH = imagesy( $src );
 /* Calculate the New Image Dimensions */
  if( $oldH > $oldW ){
   /* Portrait */
    $newW = $thumbSize;
    $newH = $oldH * ( $thumbSize / $newW );
  }else{
   /* Landscape */
    $newH = $thumbSize;
    $newW = $oldW * ( $thumbSize / $newH );
  }
 /* Create the New Image */
  $new = imagecreatetruecolor( $thumbSize , $thumbSize );
 /* Transcribe the Source Image into the New (Square) Image */
  imagecopyresampled( $new , $src , 0 , 0 , ( $newW-$thumbSize )/2 , ( $newH-$thumbSize )/2 , $thumbSize , $thumbSize , $oldW , $oldH );
  switch( $type ){
    case 'jpg' : case 'jpeg' :
      $src = imagejpeg( $new , $thumbFile ); break;
    case 'png' :
      $src = imagepng( $new , $thumbFile ); break;
    case 'gif' :
      $src = imagegif( $new , $thumbFile ); break;
  }
  imagedestroy( $new );
  imagedestroy( $src );
}

这篇关于使用PHP创建缩略图。 (裁剪到正方形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆