如何使用php调整文件夹中的图像大小?
我尝试使用此代码将图片$imgPath ="test_images/english-728b1414843000.png";
调整为200x200 px
,但不能正常工作,我该怎么做?
<?php
function resize($width, $height, $imgPath, $nm){
/* Get original image x y*/
list($w, $h) = getimagesize($_FILES[$nm]['tmp_name']);
/* calculate new image size with ratio */
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
/* new file name */
$path = $imgPath;
/* read binary data from image file */
$imgString = file_get_contents($_FILES[$nm]['tmp_name']);
/* create image from string */
$image = imagecreatefromstring($imgString);
$tmp = imagecreatetruecolor($width, $height);
//$nm = imagecreatetruecolor(400, 300);
imagealphablending( $tmp, FALSE );
imagesavealpha( $tmp, TRUE );
imagecopyresampled($tmp, $image,
0, 0,
$x, 0,
$width, $height,
$w, $h);
/* Save image */
switch ($_FILES[$nm]['type']) {
case 'image/jpeg':
imagejpeg($tmp, $path, 100);
break;
case 'image/png':
imagepng($tmp, $path, 0);
break;
case 'image/gif':
imagegif($tmp, $path);
break;
default:
exit;
break;
}
return $path;
/* cleanup memory */
imagedestroy($image);
imagedestroy($tmp);
}
?>
<?PHP
$imgPath ="test_images/english-728b1414843000.png";
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
// thumbnail sizes
$sizes = array(200 => 200);
$ext = strtolower(pathinfo($_FILES[$nm]['name'], PATHINFO_EXTENSION));
if (in_array($ext, $valid_exts)) {
/* resize image */
foreach ($sizes as $w => $h) {
$files[] = resize($w, $h, $imgPath ,$nm);
}
}
?>
答案 0 :(得分:0)
请在htacess上添加代码
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/(\d+)x(\d+)/(.*).(jpg|png)$ image.php?url=$3&ext=$4&height=$2&width=$1 [L,QSA]
create image.php and put this code--
<?php
include('smart_resize_image.function.php');
$newheight = (int) $_GET['height'];
$newwidth = (int) $_GET['width'];
$image = $_GET['url'];
$ext = $_GET['ext'];
$path = "{$image}.{$ext}";
// Content type
header('Content-Type: image/jpeg');
$resizedFile = "p/{$newwidth}x{$newheight}/{$image}.{$ext}";
mkdir(dirname($resizedFile), 0755, true);
$path = trimAndGet($path); //remove white/gray background around image
smart_resize_image($path, null, $newwidth, $newheight, true, $resizedFile, false, false, 90);
and use smart resize library
答案 1 :(得分:0)
$handle = opendir(dirname(realpath(__FILE__)) . '/thumbs/');
while ($file = readdir($handle)) {
if ($file !== '.' && $file !== '..') {
// echo '<img style="margin:10px;" src="pictures/' . $file . '" border="0" />';
$ter = 'thumbs/' . $file;
$image = strtolower($ter);
$extname = getExtension($image);
if ($extname == "jpg" || $extname == "jpeg") {
$src = imagecreatefromjpeg($ter);
} else if ($extname == "png") {
$src = imagecreatefrompng($ter);
} else {
$src = imagecreatefromgif($ter);
}
list($width, $height) = getimagesize($ter);
$newwidth = 120;
$newheight = 140;
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$filepath = $file;
$filename = 'thumbs/' . $filepath;
imagejpeg($tmp, $filename, 100);
imagedestroy($src);
imagedestroy($tmp);
}
}
function getExtension($str) {
$i = strrpos($str, ".");
if (!$i) {
return "";
}
$l = strlen($str) - $i;
$ext = substr($str, $i + 1, $l);
return $ext;
}