php图片缩放代码-按比例缩放或截取指定大小的缩略图 非常好用的一个方法_PHP教程
/**********************************************************
* function resize:
*
* = creates a resized image based on the max width
* specified as well as generates a thumbnail from
* a rectangle cut from the middle of the image.
*
* @dir = directory image is stored in
* @newdir = directory new image will be stored in
* @img = the image name
* @newimg = the image new name
* @max_w = the max width of the resized image
* @max_h = the max height of the resized image
* @th_x = The starting point of the x-coordinate
* @th_y = The starting point of the y-coordinate
* @th_w = the width of the thumbnail
* @th_h = the height of the thumbnail
* @cut = Whether to need cut out
* @center = whether cut from the middle of the image
*
**********************************************************/
function resize($dir, $newdir, $img, $newimg, $max_w, $max_h, $th_x = '', $th_y = '', $th_w = '', $th_h = '',$cut = FALSE,$center = FALSE)
{
// set destination directory
if (!$newdir) $newdir = $dir;
if (!$newimg) $newimg = $img;
// get original images width and height
list($or_w, $or_h, $or_t) = getimagesize($dir.$img);
switch($or_t){
// original image
case 1:
$or_image = imagecreatefromgif($dir.$img);
break;
case 2:
$or_image = imagecreatefromjpeg($dir.$img);
break;
case 3:
$or_image = imagecreatefrompng($dir.$img);
break;
default:
return '不支持的图像格式'; www.2cto.com
break;
}
// make sure image is a jpeg
// if ($or_t == 2) {
// obtain the image's ratio
// $ratio = ($or_h / $or_w);
$ratio = ($max_h / $max_w);
// original image
// $or_image = imagecreatefromjpeg($dir.$img);
// resize image?
if ($or_w > $max_w || $or_h > $max_h) {
// resize by height, then width (height dominant)
if ($max_h
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}
// resize by width, then height (width dominant)
else {
$rs_w = $max_w;
$rs_h = $ratio * $rs_w;
}
// copy old image to new image
$rs_image = imagecreatetruecolor($rs_w, $rs_h);
imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h);
}
// image requires no resizing
else {
$rs_w = $or_w;
$rs_h = $or_h;
$rs_image = $or_image;
}
// generate resized image
DrawImage($rs_image, $newdir.$newimg,$or_t,100);
//裁剪生成指定的大小start-------------------
if ($cut){
$th_image = imagecreatetruecolor($th_w, $th_h);
// cut out a rectangle from the resized image and store in thumbnail
if ($center){
$new_w = (($rs_w / 2) - ($th_w / 2));
$new_h = (($rs_h / 2) - ($th_h / 2));
}else {
$new_w = $th_x;
$new_h = $th_y;
}
imagecopyresampled($th_image, $rs_image, 0, 0, $new_w, $new_h, $rs_w, $rs_h, $rs_w, $rs_h);
// generate thumbnail
DrawImage($th_image, $newdir.'th_'.$newimg,$or_t,100);
@ImageDestroy($th_image);
}
//生成指定的大小end-------------------
// return true;
//}
// Image type was not jpeg!
/* else {
return false;
} */
@ImageDestroy($rs_image);
}
/**
* @resource image
* @filename
* @quality
* @type
**/
function DrawImage($resource,$filename,$type,$quality){
switch($type){
// original image
case 1:
$or_image = imagegif($resource,$filename);
break;
case 2:
$or_image = imagejpeg($resource,$filename,$quality);
break;
case 3:
$or_image = imagepng($resource,$filename);
break;
}
}
摘自 aa705123123的专栏

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

Dalam bab ini, kita akan memahami Pembolehubah Persekitaran, Konfigurasi Umum, Konfigurasi Pangkalan Data dan Konfigurasi E-mel dalam CakePHP.

PHP 8.4 membawa beberapa ciri baharu, peningkatan keselamatan dan peningkatan prestasi dengan jumlah penamatan dan penyingkiran ciri yang sihat. Panduan ini menerangkan cara memasang PHP 8.4 atau naik taraf kepada PHP 8.4 pada Ubuntu, Debian, atau terbitan mereka

Untuk bekerja dengan tarikh dan masa dalam cakephp4, kami akan menggunakan kelas FrozenTime yang tersedia.

Untuk mengusahakan muat naik fail, kami akan menggunakan pembantu borang. Di sini, adalah contoh untuk muat naik fail.

Dalam bab ini, kita akan mempelajari topik berikut yang berkaitan dengan penghalaan ?

CakePHP ialah rangka kerja sumber terbuka untuk PHP. Ia bertujuan untuk menjadikan pembangunan, penggunaan dan penyelenggaraan aplikasi lebih mudah. CakePHP adalah berdasarkan seni bina seperti MVC yang berkuasa dan mudah difahami. Model, Pandangan dan Pengawal gu

Kod Visual Studio, juga dikenali sebagai Kod VS, ialah editor kod sumber percuma — atau persekitaran pembangunan bersepadu (IDE) — tersedia untuk semua sistem pengendalian utama. Dengan koleksi sambungan yang besar untuk banyak bahasa pengaturcaraan, Kod VS boleh menjadi c

Pengesah boleh dibuat dengan menambah dua baris berikut dalam pengawal.
