PHP缩略图生成

  • php缩略图如何生成

    在PHP中,可以使用GD库或者Imagick库来生成缩略图。以下是使用GD库的示例代码:,,“php,function createThumbnail($src, $dest, $width, $height) {, list($src_w, $src_h) = getimagesize($src);, $ratio = min($width / $src_w, $height / $src_h);, $new_w = $src_w * $ratio;, $new_h = $src_h * $ratio;, $src_x = ($src_w – $new_w) / 2;, $src_y = ($src_h – $new_h) / 2;, $src_img = imagecreatefromjpeg($src);, $dest_img = imagecreatetruecolor($width, $height);, imagecopyresampled($dest_img, $src_img, 0, 0, $src_x, $src_y, $width, $height, $new_w, $new_h);, imagejpeg($dest_img, $dest);, imagedestroy($src_img);, imagedestroy($dest_img);,},,$src = ‘path/to/source/image.jpg’;,$dest = ‘path/to/destination/thumbnail.jpg’;,$width = 100;,$height = 100;,createThumbnail($src, $dest, $width, $height);,`,,这段代码定义了一个名为createThumbnail`的函数,接受源图片路径、目标缩略图路径、缩略图宽度和高度作为参数。函数内部使用GD库创建一个新的缩略图,并将其保存到指定的路径。

    2024-05-10
    048
云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购  >>点击进入