如何用PHP编写生成图片缩略图的代码?

更新于
2026-09-27 22:05:14
1阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计156个文字,预计阅读时间需要1分钟。

如何用PHP编写生成图片缩略图的代码?

python/** * 修改后的函数开头和内容 * * @filename - 图片路径 * @tmpname - 缩略图临时路径 * @xmax - 最大宽度 * @ymax - 最大高度 */function resize_image($filename, $tmpname, $xmax, $ymax) { $ext=explode('.', $filename); $ext=$ext[count($ext) - 1];}

如何用PHP编写生成图片缩略图的代码?

/********************** *@filename - path to the image *@tmpname - temporary path to thumbnail *@xmax - max width *@ymax - max height */ function resize_image($filename, $tmpname, $xmax, $ymax) { $ext = explode(".", $filename); $ext = $ext[count($ext)-1]; if($ext == "jpg" || $ext == "jpeg") $im = imagecreatefromjpeg($tmpname); elseif($ext == "png") $im = imagecreatefrompng($tmpname); elseif($ext == "gif") $im = imagecreatefromgif($tmpname); $x = imagesx($im); $y = imagesy($im); if($x <= $xmax && $y <= $ymax) return $im; if($x >= $y) { $newx = $xmax; $newy = $newx * $y / $x; } else { $newy = $ymax; $newx = $x / $y * $newy; } $im2 = imagecreatetruecolor($newx, $newy); imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y); return $im2; }

本文共计156个文字,预计阅读时间需要1分钟。

如何用PHP编写生成图片缩略图的代码?

python/** * 修改后的函数开头和内容 * * @filename - 图片路径 * @tmpname - 缩略图临时路径 * @xmax - 最大宽度 * @ymax - 最大高度 */function resize_image($filename, $tmpname, $xmax, $ymax) { $ext=explode('.', $filename); $ext=$ext[count($ext) - 1];}

如何用PHP编写生成图片缩略图的代码?

/********************** *@filename - path to the image *@tmpname - temporary path to thumbnail *@xmax - max width *@ymax - max height */ function resize_image($filename, $tmpname, $xmax, $ymax) { $ext = explode(".", $filename); $ext = $ext[count($ext)-1]; if($ext == "jpg" || $ext == "jpeg") $im = imagecreatefromjpeg($tmpname); elseif($ext == "png") $im = imagecreatefrompng($tmpname); elseif($ext == "gif") $im = imagecreatefromgif($tmpname); $x = imagesx($im); $y = imagesy($im); if($x <= $xmax && $y <= $ymax) return $im; if($x >= $y) { $newx = $xmax; $newy = $newx * $y / $x; } else { $newy = $ymax; $newx = $x / $y * $newy; } $im2 = imagecreatetruecolor($newx, $newy); imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y); return $im2; }