用PHP调整图像大小? [英] image resize with php?
问题描述
谢谢.
<?php
print"
<table <td width=\"138\" height=\"73\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td align=\"center\" valign=\"middle\"><a href=\"map.php\"><img src=\"" .
esc('img/' . $db_name . '_maps/sm' . $user['location'] . '.png') .
"\" alt=\"Map of systems around {$user['location']}\" /></a></td>
</tr>
</table>
"
?>
我的问题来自以下事实:我需要将图像提取为:
<img src=\"" .esc('img/' . $db_name . '_maps/sm' . $user['location'] . '.png') . "\" alt=\"Map of systems around {$user['location']}\" /></a>
编写了关于此的教程前一阵子.也许可以帮上忙.它从上载开始,但大部分与调整大小有关.只需通过以其他方式获取图像类型和文件名来交换$ _FILES数组的用法即可.这是您需要的代码:
// Create image from file
switch(strtolower($_FILES['image']['type']))
{
case 'image/jpeg':
$image = imagecreatefromjpeg($_FILES['image']['tmp_name']);
break;
case 'image/png':
$image = imagecreatefrompng($_FILES['image']['tmp_name']);
break;
case 'image/gif':
$image = imagecreatefromgif($_FILES['image']['tmp_name']);
break;
default:
exit('Unsupported type: '.$_FILES['image']['type']);
}
// Target dimensions
$max_width = 240;
$max_height = 180;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image,
0, 0, 0, 0,
$new_width, $new_height, $old_width, $old_height);
// Catch the imagedata
ob_start();
imagejpeg($new, NULL, 90);
$data = ob_get_clean();
// Destroy resources
imagedestroy($image);
imagedestroy($new);
// Set new content-type and status code
header("Content-type: image/jpeg", true, 200);
// Output data
echo $data;
如果要将图像存储为文件而不是将其转储到浏览器中,请移去头部和回声部分的末尾,然后将imagejpeg
调用中的NULL
参数换成实际的文件名.希望能有所帮助:)
以下是使用的代码: http://samples.geekality.net/image-resize/
ive done my research on the net and here as well and i keep on coming up with no answer that can help me: i have the below code which displays an image from a folder based on a user's location however the image is too big and i need to resize it. all the scripts that i have tried or read relate to files being uploaded. can anyone push me in the right direction?
thank you.
<?php
print"
<table <td width=\"138\" height=\"73\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td align=\"center\" valign=\"middle\"><a href=\"map.php\"><img src=\"" .
esc('img/' . $db_name . '_maps/sm' . $user['location'] . '.png') .
"\" alt=\"Map of systems around {$user['location']}\" /></a></td>
</tr>
</table>
"
?>
My problem arises from the fact that i need to pull the images as:
<img src=\"" .esc('img/' . $db_name . '_maps/sm' . $user['location'] . '.png') . "\" alt=\"Map of systems around {$user['location']}\" /></a>
Wrote a tutorial about this a while ago. Perhaps it can help. It starts with uploading, but most of it is about resizing. Just swap out the usage of the $_FILES array by geting the image type and file name a different way. Here's the code you should need:
// Create image from file
switch(strtolower($_FILES['image']['type']))
{
case 'image/jpeg':
$image = imagecreatefromjpeg($_FILES['image']['tmp_name']);
break;
case 'image/png':
$image = imagecreatefrompng($_FILES['image']['tmp_name']);
break;
case 'image/gif':
$image = imagecreatefromgif($_FILES['image']['tmp_name']);
break;
default:
exit('Unsupported type: '.$_FILES['image']['type']);
}
// Target dimensions
$max_width = 240;
$max_height = 180;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image,
0, 0, 0, 0,
$new_width, $new_height, $old_width, $old_height);
// Catch the imagedata
ob_start();
imagejpeg($new, NULL, 90);
$data = ob_get_clean();
// Destroy resources
imagedestroy($image);
imagedestroy($new);
// Set new content-type and status code
header("Content-type: image/jpeg", true, 200);
// Output data
echo $data;
If you want to store the image as a file rather than dumping it to the browser, remove the head and echo part at the end and then swap out the NULL
parameter in the imagejpeg
call with an actual filename. Hope that helps :)
Here's the code in use: http://samples.geekality.net/image-resize/
这篇关于用PHP调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!