gpt4 book ai didi

php - 使用 PHP 创建缩略图。 (裁剪成正方形)

转载 作者:可可西里 更新时间:2023-10-31 23:51:45 24 4
gpt4 key购买 nike

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

这是我现在使用的:

    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);
}

我将如何改变它来完成我想要的(方形拇指)?

提前致谢。

最佳答案

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 - 使用 PHP 创建缩略图。 (裁剪成正方形),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2686000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com