ESSAY / NOTE

imagemagic 缩放指定大小 智能剪裁 不变形

convert 转换图像格式和大小,模糊,裁剪,驱除污点,抖动,临近,图片上画图片,加入新图片,生成缩略图等。 identify 描述一个或较多图像文件的格式和特性。 composite 根据一个图片或多个图片组合生成图片   举个例子 convert +profile '*' [src]{file}.{ext} -quality 80 -resize '280x140^>' -gravity Center -crop 280x140+0+0 +repage [out]{file}_280x140.{ext} 把一张图片按80的质量去压缩(jpg的压缩参数),同时按图片比例非强制缩放成不超过280x140的图片.居中裁剪280x140,去掉图片裁减后的空白和图片exif信息,通常这种指令是为了保证图片大小正好为280x140   下面对各个指令的含义简要说明   -quality   图片质量,jpg默认99,png默认75   -resize 100x100      高度和宽度比例保留最高值,高比不变 100x100^     高度和宽度比例保留最低值,宽高比不变 100x100!      宽度和高度强制转换,忽视宽高比 100x100>     更改长宽,当图片长或宽超过规定的尺寸 100x100<     更改长宽 只有当图片长宽都超过规定的尺寸 100x100^>   更改长宽,当图片长或宽超过规定的尺寸。高度和宽度比例保留最低值 100x100^<   更改长宽,只有当图片长宽都超过规定的尺寸。高度和宽度比例保留最低值 100             按指定的宽度缩放,保持宽高比例 x100          按指定高度缩放,保持宽高比   -gravity NorthWest, North, NorthEast, West, Center, East,  SouthWest, South, SouthEast截取用的定位指令,定位截取区域在图片中的方位   -crop 200x200+0+0 截取用的截取指令 ,在用定位指令后,按后两位的偏移值偏移截取范围左上角的像素后,再按前两位的数值,从左上角开始截取相应大小的图片   +repage 去掉图片裁减后的空白 -dissolve 30 设定组合图片透明度dissolve示例 +/-profile * 去掉/添加图片exif信息 下面记录下一些比较复杂一点的指令: convert +profile '*' [src]{file}.{ext} -quality 90 -resize '700>' /data/tony/watermark_1.png -gravity southeast -compose Dissolve -composite [out]{file}_1024x1024.{ext} convert +profile '*' [src]{file}.{ext} /data/watermark_1.png -gravity southeast -geometry +10+10 -composite [src]{file}.{ext} convert +profile '*' [src]{file}.{ext} -coalesce -resize '950x135>' [out]{file}_950x135.{ext} convert +profile '*' [src]{file}.{ext} -resize '650x500>' miff:- | composite +profile '*' -dissolve 30 -gravity southeast /data/tony/watermark_1.png - [out]{file}_650x500.{ext} convert [src]{file}.{ext} -quality 80 -resize '190>' -background white -gravity center -extent 190x +repage [out]{file}_width190.{ext} 【/bin/sh^M: bad interpreter: 没有那个文件或目录】 vim filename 然后用命令 :set ff? #可以看到dos或unix的字样. 如果的确是dos格式的。 然后用 :set ff=unix #把它强制为unix格式的, 然后存盘退出。 ############################################### shell 除法计算 shell计算中使用除法,基本默认上都是整除。 比如: num1=2 num2=3 num3=`expr $num1 / $num2` 这个时候num3=0 ,是因为是因为expr不支持浮点除法 解决的方法: num3=`echo "scale=2; $num1/$num2" | bc` 使用bc工具,sclae控制小数点后保留几位 还有一种方法 awk 'BEGIN{printf "%.2f\n",’$num1‘/’$num2‘}' 如果用百分比表示 awk 'BEGIN{printf "%.2f%\n",(’$num1‘/’$num2‘)*100}' ############################################## convert +profile '*' -size 950x135 xc:#ffffff [src]{file}.{ext} -resize 950x135 -gravity center -compose Src-over -composite [out]{file}_950x135.{ext} 解读:该指令会把gif的动态图拆分成好几张图, -coalesce 完全定义一个GIF动画序列的每一帧的外观,形成“电影胶片”动画 根据它的-处置元数据覆盖各图像中的图像序列,以再现一个动画中的动画序列中的每个点的外观。有的图片应该是相同的大小,并且为动画继续如预期般GIF动画的工作分配适当的GIF处理设置。这样的帧更容易查看和比高度优化的GIF图像叠加处理。 该动画可以重新优化使用层法“优化”处理后,虽然没有保证恢复的GIF动画优化是比原来更好。 ################################################## gif拿到的只是最后一帧的数据,所以gif的图片在width和height值上有出入 convert +profile '*' tly2.gif -resize "600>" tly2_600.gif C:\>identify tly2.gif tly2.gif[0] GIF 341x186 341x186+0+0 8-bit sRGB 256c 191KB 0.000u 0:00.003 tly2.gif[1] GIF 194x173 341x186+128+13 8-bit sRGB 256c 191KB 0.000u 0:00.010 tly2.gif[2] GIF 192x175 341x186+130+11 8-bit sRGB 256c 191KB 0.000u 0:00.013 tly2.gif[3] GIF 80x177 341x186+131+9 8-bit sRGB 256c 191KB 0.000u 0:00.016 ... tly2.gif[30] GIF 164x175 341x186+122+11 8-bit sRGB 256c 191KB 0.000u 0:00.094 tly2.gif[31] GIF 145x172 341x186+122+14 8-bit sRGB 256c 191KB 0.000u 0:00.097 tly2.gif[32] GIF 195x167 341x186+122+19 8-bit sRGB 256c 191KB 0.000u 0:00.100 tly2.gif[33] GIF 196x174 341x186+121+12 8-bit sRGB 256c 191KB 0.000u 0:00.105   一、crop_center.sh 如果原图宽>高:按宽*size/高缩放; 如果原图宽<高:按高*size/宽缩放; 并居中裁剪size*size   复制代码
  1. #!/bin/bash
  2. export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
  3. filename=$1
  4. filename2=$2
  5. size=$3
  6. resize="70"
  7. wh=($(/usr/local/bin/identify -format "%w %h " $1))
  8. if [ ${wh[0]} -gt ${wh[1]} ]
  9. then
  10. resize=$(echo "${wh[0]}*$size/${wh[1]}+1" | bc)
  11. else
  12. resize=$(echo "${wh[1]}*$size/${wh[0]}+1" | bc)
  13. fi
  14. /usr/local/bin/convert +profile "*" -quality 75 -resize ${resize}x${resize} $filename $filename2
  15. /usr/local/bin/convert +profile "*" -gravity Center -crop ${size}x${size}+0+0 $filename2 $filename2
复制代码 使用范例:/usr/local/bin/crop_center.sh [src]{file}.{ext} [out]{file}_small.{ext} 100   二、tony-convert-1.sh 原图按1024x1024>等比缩放,并打水印于右下角;如果宽高都小于300,则直接使用原图   复制代码
  1. #!/bin/sh
  2. # we have 2 arguments: source file name and dest file name
  3. if [ $# -ne 2 ] ; then
  4.   echo "Usage: tony-convert-1.sh [source path] [dest path]"
  5.   echo "Example: ./tony-convert-1.sh src/1234.jpg out/1234_100x200.jpg"
  6.   exit 1
  7. fi
  8. srcFile="$1"
  9. dstFile="$2"
  10. index1=`expr index "${srcFile}" /`
  11. index2=`expr index "${srcFile}" .`
  12. file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
  13. ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
  14. #identify image num
  15. picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
  16. echo animate pictures: ${picCount}
  17. if [ ${picCount} -gt 1 ] ; then
  18.   echo "No handle for animate pictures"
  19.   cp ${srcFile} ${dstFile}
  20.   exit 0
  21. fi
  22. #identify image size
  23. sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h %f\n" ${srcFile}`
  24. index1=`expr index "${sizeMatrix}" x`
  25. width=`expr substr "${sizeMatrix}" 1 \`expr ${index1} - 1\``
  26. echo width: ${width}
  27. index2=`expr \`expr index "${sizeMatrix}" ' '\` - ${index1} - 1`
  28. height=`expr substr "${sizeMatrix}" \`expr ${index1} + 1\` ${index2}`
  29. echo height: ${height}
  30. if [ ${width} -lt 300 ] ; then
  31.   if [ ${height} -lt 300 ] ; then
  32.     echo Image size less than 300x300
  33.     cp ${srcFile} ${dstFile}
  34.     exit 0
  35.   fi
  36. fi
  37. if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then
  38.   index1=`expr index "${dstFile}" .`
  39.   dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg
  40. fi
  41. #convert image/usr/local/sbin/bin/bin/convert +profile '*' ${srcFile} -resize '1024x1024>' /data/tony/watermark_1.png -gravity southeast -compose Dissolve -composite  ${dstFile}
复制代码 使用范例:/usr/local/sbin/bin/bin/tony-convert-1.sh [src]{file}.{ext} [out]{file}_1024x1024.{ext}   三、resizeByWidthOrHeight.sh: 宽大于高时:定宽等比缩放 高自适应;宽小于高时:定高等比缩放,宽自适应   复制代码
  1. #!/bin/sh
  2. # we have 2 arguments: source file name and dest file name
  3. if [ $# -ne 3 ] ; then
  4.   echo "Usage: resizeByWidthOrHeight.sh [source path] [widthxheigth] [dest path]"
  5.   echo "Example: ./resizeByWidthOrHeight.sh src/1234.jpg 150x150 out/1234_150x2150.jpg"
  6.   exit 1
  7. fi
  8. srcFile="$1"
  9. dstFile="$3"
  10. destSize="$2"
  11. destWidth=${destSize%x*}
  12. destHeight=${destSize#*x}
  13. echo "destSize: ${destWidth}x${destHeight}"
  14. index1=`expr index "${srcFile}" /`
  15. echo "index1: $index1"
  16. index2=`expr index "${srcFile}" .`
  17. echo "index2: $index2"
  18. file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
  19. echo "file: $file"
  20. ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
  21. echo "ext: $ext"
  22. #identify image num
  23. picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
  24. echo animate pictures: ${picCount}
  25. if [ ${picCount} -gt 1 ] ; then
  26.   echo "No handle for animate pictures"
  27.   cp ${srcFile} ${dstFile}
  28.   exit 0
  29. fi
  30. #identify image size
  31. sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}`
  32. echo "index1: $index1"
  33. width=${sizeMatrix%x*}
  34. echo width: ${width}
  35. height=${sizeMatrix#*x}
  36. echo height: ${height}
  37. newSize="${destWidth}x${destHeight}"
  38. # width < height
  39. if [ ${width} -lt ${height} ] ; then
  40.   newSize="'x${destHeight}>'"
  41. else
  42.   newSize="'${destWidth}>'"
  43. fi
  44. echo "newSize: ${newSize}"
  45. # resize
  46. resizeCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 100 -resize ${newSize} ${dstFile}"
  47. echo "resizeCmd: ${resizeCmd}"
  48. eval "${resizeCmd}"
复制代码 使用范例:/usr/local/sbin/bin/bin/resizeByWidthOrHeight.sh [src]{file}.{ext} 594x395 [out]{file}_594x395.{ext}   四、tony-convert-2.sh: 缩放规则: 目标图宽<高时:定高等比压缩,宽自适应; 原图宽高比>目标图宽高比时:定高等比压缩,宽自适应; 否则:定宽等比压缩,高自适应 裁剪规则:默认居中,若目标图宽>=高,则居左上角   复制代码
  1. #!/bin/sh
  2. # we have 2 arguments: source file name and dest file name
  3. if [ $# -ne 3 ] ; then
  4.   echo "Usage: tony-convert-2.sh [source path] [widthxheigth] [dest path]"
  5.   echo "Example: ./tony-convert-2.sh src/1234.jpg 150x150 out/1234_150x150.jpg"
  6.   exit 1
  7. fi
  8. srcFile="$1"
  9. dstFile="$3"
  10. destSize="$2"
  11. destWidth=${destSize%x*}
  12. destHeight=${destSize#*x}
  13. echo "destSize: ${destWidth}x${destHeight}"
  14. index1=`expr index "${srcFile}" /`
  15. echo "index1: $index1"
  16. index2=`expr index "${srcFile}" .`
  17. echo "index2: $index2"
  18. file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
  19. echo "file: $file"
  20. ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
  21. echo "ext: $ext"
  22. #identify image num
  23. picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
  24. echo animate pictures: ${picCount}
  25. if [ ${picCount} -gt 1 ] ; then
  26.   echo "No handle for animate pictures"
  27.   cp ${srcFile} ${dstFile}
  28.   exit 0
  29. fi
  30. #identify image size
  31. sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}`
  32. echo "index1: $index1"
  33. width=${sizeMatrix%x*}
  34. echo width: ${width}
  35. height=${sizeMatrix#*x}
  36. echo height: ${height}
  37. if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then
  38.   index1=`expr index "${dstFile}" .`
  39.   dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg
  40. fi
  41. srcRate=$(echo "scale=9; $width*1.0 / $height*1.0" | bc)
  42. destRate=$(echo "scale=9; $destWidth*1.0 / $destHeight*1.0" | bc)
  43. echo "srcRate: ${srcRate}"
  44. echo "destRate: ${destRate}"
  45. resizeCmd="/usr/local/sbin/bin/bin/convert +profile '*' ${srcFile} -resize '${destWidth}x${destHeight}^>' -grAvity Center -crop ${destWidth}x${destHeight}+0+0 +repage ${dstFile}"
  46. newSize="${destWidth}x${destHeight}"
  47. # width < height
  48. if [ ${destWidth} -lt ${destHeight} ] ; then
  49.   newSize="x${destHeight}>"
  50. elif [ `echo "${srcRate} > ${destRate}" | bc` -eq 1 ] ; then
  51.   newSize="x${destHeight}>"
  52. else
  53.   newSize="${destWidth}>"
  54. fi
  55. grAvityWay="Center"
  56. # width < height
  57. if [ ${destWidth} -ge ${destHeight} ] ; then
  58.   grAvityWay="NorthWest"
  59. fi
  60. # resize
  61. resizeCmd="/usr/local/sbin/bin/bin/convert +profile '*' ${srcFile} -resize '${newSize}' -grAvity ${grAvityWay} -crop ${destWidth}x${destHeight}+0+0 +repage ${dstFile}"
  62. echo "resizeCmd: ${resizeCmd}"
  63. eval "${resizeCmd}"
复制代码 使用范例:/usr/local/sbin/bin/bin/tony-convert-2.sh [src]{file}.{ext} 170x105 [out]{file}_170x105.{ext}   五、tony-convert-3.sh: 以destWidth表示目的图宽,destHeight表示目的图高,persentSize=destWidth/destHeight表示目的图宽高比; 当原图宽<destWidth且高<destHeight时,不截取,直接上传;  当原图宽<destWidth,高>destHeight时,以原图宽为宽居中裁剪 竖图;  当原图宽>destWidth,高<destHeight时,以原图高为高居中裁剪 横图;  当原图宽>destWidth,高>destHeight,  { 当原图宽/高>persentSize时,将原图高压缩为destHeight居中裁剪  当原图宽/高<persentSize时,将原图宽压缩为destWidth居中裁剪 }   复制代码
  1. !/bin/sh
  2. # we have 2 arguments: source file name and dest file name
  3. if [ $# -ne 3 ] ; then
  4.   echo "Usage: tony-convert-3.sh [source path] [widthxheigth] [dest path]"
  5.   echo "Example: ./tony-convert-3.sh src/1234.jpg 750x375 out/1234_750x375.jpg"
  6.   exit 1
  7. fi
  8. srcFile="$1"
  9. dstFile="$3"
  10. destSize="$2"
  11. destWidth=${destSize%x*}
  12. destHeight=${destSize#*x}
  13. echo "destSize: ${destWidth}x${destHeight}"
  14. index1=`expr index "${srcFile}" /`
  15. echo "index1: $index1"
  16. index2=`expr index "${srcFile}" .`
  17. echo "index2: $index2"
  18. file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
  19. echo "file: $file"
  20. ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
  21. echo "ext: $ext"
  22. #identify image num
  23. picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
  24. echo animate pictures: ${picCount}
  25. if [ ${picCount} -gt 1 ] ; then
  26.   echo "No handle for animate pictures"
  27.   cp ${srcFile} ${dstFile}
  28.   exit 0
  29. fi
  30. #identify image size
  31. sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}`
  32. echo "index1: $index1"
  33. width=${sizeMatrix%x*}
  34. echo width: ${width}
  35. height=${sizeMatrix#*x}
  36. echo height: ${height}
  37. #if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then
  38.   #index1=`expr index "${dstFile}" .`
  39.   #dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg 
  40. #fi
  41. #newSize="${destWidth}x${destHeight}"
  42. heightPadding="0"
  43. widthPadding="0"
  44. persentSize=`echo "scale=4; ${destWidth}/${destHeight}" | bc`
  45. echo "persentSize: ${persentSize}"
  46. # quality
  47. qualityCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 90 ${dstFile}"
  48. echo "qualityCmd: ${qualityCmd}"
  49. eval "${qualityCmd}"
  50. # width > 750 && height <375  hengTu
  51. if [ ${width} -gt ${destWidth} ] && [ ${height} -lt ${destHeight} ] ; then
  52.   heightPadding=$[ ${destHeight}/2-${height}/2]
  53.   echo heightPadding: ${heightPadding}
  54.   flag=0
  55. # width < 750 && height >375  shuTu
  56. elif [ ${width} -lt ${destWidth} ] && [ ${height} -gt ${destHeight} ] ; then
  57.   widthPadding=$[ ${destWidth}/2-${width}/2 ]
  58.   echo widthPadding: ${widthPadding}
  59.   flag=0
  60. # width > 750 && height >375  resize and crop
  61. elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ $ [ ${width}/${height} ]>${persentSize} ]  ; then
  62.   echo "resize by src's height then crop"
  63.   newSize="'x${destHeight}>'"
  64.   flag=1
  65. elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ $[ ${width}/${height} ]<${persentSize} ]  ; then
  66.   echo "resize by src's width then crop"
  67.   newSize="'${destWidth}>'"
  68.   flag=1
  69. fi
  70. echo "newSize: ${newSize}"
  71. # resize
  72. if [ ${flag} -eq 1 ] ; then
  73.   resizeCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -resize ${newSize} ${dstFile}"
  74.   echo "resizeCmd: ${resizeCmd}"
  75.   eval "${resizeCmd}"
  76.   cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center  -crop ${destSize}+0+0 +repage ${dstFile}"
  77. else
  78.   echo "only crop pic"
  79.   cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center  -crop ${destSize}+${widthPadding}+${heightPadding} +repage ${dstFile}"
  80. fi
  81. # crop
  82. echo "cropCmd: ${cropCmd}"
  83. eval "${cropCmd}"
复制代码 使用范例:/usr/local/sbin/bin/bin/tony-convert-3.sh [src]{file}.{ext} 750x375 [out]{file}_750x375.{ext}   六、tony-convert-3_bg.sh: 以destWidth表示目的图宽,destHeight表示目的图高, 当原图宽<destWidth且高<destHeight时,不截取,直接上传; 当原图宽<destWidth,高>destHeight时,以原图宽为宽居中裁剪 树图; 当原图宽>destWidth,高<destHeight时,以原图高为高居中裁剪 横图; 当原图宽>destWidth,高>destHeight, { 当原图宽>高时,将原图高压缩为destHeight居中裁剪 当原图宽<高时,将原图宽压缩为destWidth居中裁剪 } 所有情况都要居中补白   复制代码
  1. !/bin/sh
  2. # we have 2 arguments: source file name and dest file name
  3. if [ $# -ne 3 ] ; then
  4.   echo "Usage: tony-convert-3_bg.sh [source path] [widthxheigth] [dest path]"
  5.   echo "Example: ./tony-convert-3_bg.sh src/1234.jpg 750x375 out/1234_750x375.jpg"
  6.   exit 1
  7. fi
  8. srcFile="$1"
  9. dstFile="$3"
  10. destSize="$2"
  11. destWidth=${destSize%x*}
  12. destHeight=${destSize#*x}
  13. echo "destSize: ${destWidth}x${destHeight}"
  14. index1=`expr index "${srcFile}" /`
  15. echo "index1: $index1"
  16. index2=`expr index "${srcFile}" .`
  17. echo "index2: $index2"
  18. file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
  19. echo "file: $file"
  20. ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
  21. echo "ext: $ext"
  22. #identify image num
  23. picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
  24. echo animate pictures: ${picCount}
  25. if [ ${picCount} -gt 1 ] ; then
  26.   echo "No handle for animate pictures"
  27.   cp ${srcFile} ${dstFile}
  28.   exit 0
  29. fi
  30. #identify image size
  31. sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}`
  32. echo "index1: $index1"
  33. width=${sizeMatrix%x*}
  34. echo width: ${width}
  35. height=${sizeMatrix#*x}
  36. echo height: ${height}
  37. # quality
  38. qualityCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 90 ${dstFile}"
  39. echo "qualityCmd: ${qualityCmd}"
  40. eval "${qualityCmd}"
  41. # width > 750 && height <375  hengTu
  42. flag=0
  43. if [ ${width} -gt ${destWidth} ] && [ ${height} -lt ${destHeight} ] ; then
  44.   heightPadding=$[ ${destHeight}/2-${height}/2]
  45.   echo heightPadding: ${heightPadding}
  46.   flag=0
  47. # width < 750 && height >375  shuTu
  48. elif [ ${width} -lt ${destWidth} ] && [ ${height} -gt ${destHeight} ] ; then
  49.   widthPadding=$[ ${destWidth}/2-${width}/2 ]
  50.   echo widthPadding: ${widthPadding}
  51.   flag=0
  52. # width > 750 && height >375  resize and crop
  53. elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ ${width} -gt ${height} ]  ; then
  54.   echo "resize by src's height then crop"
  55.   newSize="'x${destHeight}>'"
  56.   flag=1
  57. elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ ${width} -lt ${height} ]  ; then
  58.   echo "resize by src's width then crop"
  59.   newSize="'${destWidth}>'"
  60.   flag=1
  61. fi
  62. echo "newSize: ${newSize}"
  63. # resize
  64. if [ ${flag} -eq 1 ] ; then
  65.   resizeCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -resize ${newSize} ${dstFile}"
  66.   echo "resizeCmd: ${resizeCmd}"
  67.   eval "${resizeCmd}"
  68.   cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center -crop ${destSize}+0+0 +repage -background white -compose Copy -gravity center -extent ${destSize} ${dstFile}"
  69. else
  70.   echo "only crop pic"
  71.   cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center -crop ${destSize}+0+0 +repage -background white -compose Copy -gravity center -extent ${destSize} ${dstFile}"
  72. fi
  73. # crop
  74. echo "cropCmd: ${cropCmd}"
  75. eval "${cropCmd}"
复制代码 使用范例:【240x240大小,背景补白,1PX灰色边框】 /usr/local/sbin/bin/bin/tony-convert-3_bg.sh [src]{file}.{ext} 240x240 [out]{file}_240x240.{ext}   七、tony-convert-4.sh: 原图宽大于destWidth: { 高>destHeight时:定宽等比缩放 以原图高为高居中裁剪; 否则:从左上角裁剪 } 原图宽小于destWidth: { 高>destHeight时:以原图高为高居中裁剪; 否则:从左上角裁剪 }   复制代码
  1. #!/bin/sh
  2. # we have 2 arguments: source file name and dest file name
  3. if [ $# -ne 3 ] ; then
  4.   echo "Usage: tony-convert-4.sh [source path] [widthxheigth] [dest path]"
  5.   echo "Example: ./tony-convert-4.sh src/1234.jpg 150x150 out/1234_150x150.jpg"
  6.   exit 1
  7. fi
  8. srcFile="$1"
  9. dstFile="$3"
  10. destSize="$2"
  11. destWidth=${destSize%x*}
  12. destHeight=${destSize#*x}
  13. echo "destSize: ${destWidth}x${destHeight}"
  14. index1=`expr index "${srcFile}" /`
  15. echo "index1: $index1"
  16. index2=`expr index "${srcFile}" .`
  17. echo "index2: $index2"
  18. file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
  19. echo "file: $file"
  20. ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
  21. echo "ext: $ext"
  22. #identify image num
  23. picCount=`/usr/local/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
  24. echo animate pictures: ${picCount}
  25. if [ ${picCount} -gt 1 ] ; then
  26.   echo "No handle for animate pictures"
  27.   cp ${srcFile} ${dstFile}
  28.   exit 0
  29. fi
  30. #identify image size
  31. sizeMatrix=`/usr/local/bin/identify -format "%wx%h" ${srcFile}`
  32. echo "index1: $index1"
  33. width=${sizeMatrix%x*}
  34. echo width: ${width}
  35. height=${sizeMatrix#*x}
  36. echo height: ${height}
  37. #if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then
  38.   #index1=`expr index "${dstFile}" .`
  39.   #dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg 
  40. #fi
  41. newSize="${destWidth}x${destHeight}"
  42. # width < height
  43. if [ ${width} -gt ${destWidth} ] ; then
  44.   newSize="'${destWidth}>'"
  45.   if [ ${height} -gt ${destHeight}] ; then
  46.   heihgtPadding="'${destHeight}/2'"
  47.   else
  48.   heihgtPadding="'0'"
  49.   fi
  50. else
  51.   newSize="'${width}>'"
  52.   if [ ${height} -gt ${destHeight}] ; then
  53.     heihgtPadding="'${destHeight}/2'"
  54.   else
  55.     heihgtPadding="'0'"
  56.   fi
  57. fi
  58. echo "newSize: ${newSize}"
  59. # resize
  60. resizeCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 90 -resize ${newSize} ${dstFile}"
  61. echo "resizeCmd: ${resizeCmd}"
  62. eval "${resizeCmd}"
  63. # add white ground
  64. addWhiteCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -crop ${destSize}+0-${heihgtPadding} +repage ${dstFile}"
  65. echo "addWhiteCmd: ${addWhiteCmd}"
  66. eval "${addWhiteCmd}"
复制代码 使用范例:【160*120裁剪或居中加水印】/usr/local/sbin/bin/bin/tony-convert-4.sh [src]{file}.{ext} 160x120 [out]{file}_160.{ext}   八、tony-convert-5.sh: 宽大于高时:定宽等比缩放 高自适应; 宽小于高时:定高等比缩放,宽自适应; 并居中补白   复制代码
  1. #!/bin/sh
  2. # we have 2 arguments: source file name and dest file name
  3. if [ $# -ne 3 ] ; then
  4.   echo "Usage: tony-convert-5.sh [source path] [widthxheigth] [dest path]"
  5.   echo "Example: ./tony-convert-5.sh src/1234.jpg 150x150 out/1234_150x2150.jpg"
  6.   exit 1
  7. fi
  8. srcFile="$1"
  9. dstFile="$3"
  10. destSize="$2"
  11. destWidth=${destSize%x*}
  12. destHeight=${destSize#*x}
  13. echo "destSize: ${destWidth}x${destHeight}"
  14. index1=`expr index "${srcFile}" /`
  15. echo "index1: $index1"
  16. index2=`expr index "${srcFile}" .`
  17. echo "index2: $index2"
  18. file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
  19. echo "file: $file"
  20. ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
  21. echo "ext: $ext"
  22. #identify image num
  23. picCount=`/usr/local/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
  24. echo animate pictures: ${picCount}
  25. if [ ${picCount} -gt 1 ] ; then
  26.   echo "No handle for animate pictures"
  27.   cp ${srcFile} ${dstFile}
  28.   exit 0
  29. fi
  30. #identify image size
  31. sizeMatrix=`/usr/local/bin/identify -format "%wx%h" ${srcFile}`
  32. echo "index1: $index1"
  33. width=${sizeMatrix%x*}
  34. echo width: ${width}
  35. height=${sizeMatrix#*x}
  36. echo height: ${height}
  37. #if [ ${width} -lt 300 ] ; then
  38. #  if [ ${height} -lt 300 ] ; then
  39. #    echo Image size less than 300x300
  40. #    cp ${srcFile} ${dstFile}
  41. #    exit 0
  42. #  fi
  43. #fi
  44. if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then
  45.   index1=`expr index "${dstFile}" .`
  46.   dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg
  47. fi
  48. newSize="${destWidth}x${destHeight}"
  49. # width < height
  50. if [ ${width} -lt ${height} ] ; then
  51.   newSize="'x${destHeight}>'"
  52. else
  53.   newSize="'${destWidth}>'"
  54. fi
  55. echo "newSize: ${newSize}"
  56. # resize
  57. resizeCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 80 -resize ${newSize} ${dstFile}"
  58. echo "resizeCmd: ${resizeCmd}"
  59. eval "${resizeCmd}"
  60. # add white ground
  61. addWhiteCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -background white -gravity center -extent ${destSize} -gravity Center -crop ${destSize}+0+0 +repage ${dstFile}"
  62. echo "addWhiteCmd: ${addWhiteCmd}"
  63. eval "${addWhiteCmd}"
复制代码 使用范例:【正方形图】/usr/local/sbin/bin/bin/tony-convert-5.sh [src]{file}.{ext} 90x90 [out]{file}_90x90.{ext}
转载于:https://my.oschina.net/china008/blog/426148