我有这个PHP功能,根据我需要它和我希望显示的大小调整图像大小。 但我的问题是如何将其设置为在html图像元素中显示已调整大小的图像而不将php内容类型设置为图像? 如果我将内容类型设置为图像,它将在整个页面中显示图像,如果我删除它将输出长的未知字符,我该怎么做才能得到我想要的?
<?php
function CroppedThumbnail($imagename, $imgwidth, $imgheight){
// Set a maximum height and width
$width = $imgwidth;
$height = $imgheight;
// Content type
//header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($imagename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($imagename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, $imagename, 100);
return $imagename;
}
$filename1 = 'http://static1.techlosofy.com/wp-content/uploads/YouTube-Logo-Large.jpg';
$filename2 = 'http://static1.techlosofy.com/wp-content/uploads/YouTube-Logo-Small.jpg';
echo '<img data="image1" src="'.CroppedThumbnail($filename1, 100, 100).'"/>';
echo '<img data="image2" src="'.CroppedThumbnail($filename2, 100, 100).'"/>';
?>
我想调整我在
上调用此功能的任何图像的大小答案 0 :(得分:1)
$newFile = 'images/newFile.jpg';
imagejpeg($image_p, $newFile, 100);
return '/' . $newFile;
我已编辑了您的代码:
<?php
function CroppedThumbnail($imagename, $imgwidth, $imgheight){
// Set a maximum height and width
$width = $imgwidth;
$height = $imgheight;
// Content type
//header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($imagename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($imagename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
$newFileName = 'images/newFile.jpg';
imagejpeg($image_p, $newFileName, 100);
return '/' . $newFileName;
}
$filename1 = 'http://static1.techlosofy.com/wp-content/uploads/YouTube-Logo-Large.jpg';
echo '<img data="image1" src="'.CroppedThumbnail($filename1, 100, 100).'"/>';
?>
答案 1 :(得分:1)
我找到了完成这项工作的数学方法
Github repo - https://github.com/gayanSandamal/easy-php-image-resizer
直播示例 - https://plugins.nayague.com/easy-php-image-resizer/
<?php
//path for the image
$source_url = '2018-04-01-1522613288.PNG';
//separate the file name and the extention
$source_url_parts = pathinfo($source_url);
$filename = $source_url_parts['filename'];
$extension = $source_url_parts['extension'];
//define the quality from 1 to 100
$quality = 10;
//detect the width and the height of original image
list($width, $height) = getimagesize($source_url);
$width;
$height;
//define any width that you want as the output. mine is 200px.
$after_width = 200;
//resize only when the original image is larger than expected with.
//this helps you to avoid from unwanted resizing.
if ($width > $after_width) {
//get the reduced width
$reduced_width = ($width - $after_width);
//now convert the reduced width to a percentage and round it to 2 decimal places
$reduced_radio = round(($reduced_width / $width) * 100, 2);
//ALL GOOD! let's reduce the same percentage from the height and round it to 2 decimal places
$reduced_height = round(($height / 100) * $reduced_radio, 2);
//reduce the calculated height from the original height
$after_height = $height - $reduced_height;
//Now detect the file extension
//if the file extension is 'jpg', 'jpeg', 'JPG' or 'JPEG'
if ($extension == 'jpg' || $extension == 'jpeg' || $extension == 'JPG' || $extension == 'JPEG') {
//then return the image as a jpeg image for the next step
$img = imagecreatefromjpeg($source_url);
} elseif ($extension == 'png' || $extension == 'PNG') {
//then return the image as a png image for the next step
$img = imagecreatefrompng($source_url);
} else {
//show an error message if the file extension is not available
echo 'image extension is not supporting';
}
//HERE YOU GO :)
//Let's do the resize thing
//imagescale([returned image], [width of the resized image], [height of the resized image], [quality of the resized image]);
$imgResized = imagescale($img, $after_width, $after_height, $quality);
//now save the resized image with a suffix called "-resized" and with its extension.
imagejpeg($imgResized, $filename . '-resized.'.$extension);
//Finally frees any memory associated with image
//**NOTE THAT THIS WONT DELETE THE IMAGE
imagedestroy($img);
imagedestroy($imgResized);
}
?>
答案 2 :(得分:0)
我建议您对代码进行简单的更改,以满足您的要求,并且可以重复使用。
这里我们可以将调整大小功能分成单独的文件。
resize.php
<?php
function CroppedThumbnail($imagename, $imgwidth, $imgheight){
// Set a maximum height and width
$width = $imgwidth;
$height = $imgheight;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($imagename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($imagename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
return imagejpeg($image_p, null, 100);
}
header('Content-Type: image/jpeg');
echo CroppedThumbnail($_GET['imagename'], $_GET['width'], $_GET['height'])
?>
现在我们可以使用上面的resize文件来渲染调整大小的图像,方法是将图像名称,高度和宽度传递给请求 使用example.php
<?php
$filename1 = 'path and file name of image 1 here';
$filename2 = 'path and file name of image 2 here';
echo '<img data="image1" src="imagetest.php?imagename='. $filename1 .'&height=100&width=100"/>';
echo '<img data="image2" src="imagetest.php?imagename='. $filename2 .'&height=100&width=100"/>';
?>