用php脚本调整图像大小
我正在尝试调整我用表单上传的图像的大小。我在这里使用脚本场景: 我还使用以下代码上传图像: upload.php:用php脚本调整图像大小,php,image,Php,Image,我正在尝试调整我用表单上传的图像的大小。我在这里使用脚本场景: 我还使用以下代码上传图像: upload.php: <?php session_start(); require_once "database.php"; db_connect(); require_once "auth.php"; $current_user = current_user(); $emailstring = $current_user['email'
<?php
session_start();
require_once "database.php";
db_connect();
require_once "auth.php";
$current_user = current_user();
$emailstring = $current_user['email'];
//Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
//This is an array that holds all the valid image MIME types
$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");
if (in_array($file['type'], $valid_types))
return 1;
return 0;
}
function showContents($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
//Set some constants
//This variable is the path to the image folder where all the images are going to be stored
//Note that there is a trailing forward slash
$TARGET_PATH = "profile_images/";
//Get our POSTed variables
$upload_picture_fileinput = $_FILES['upload_picture_fileinput'];
//Sanitize input
$upload_picture_fileinput['name'] = mysql_real_escape_string($upload_picture_fileinput['name']);
//Build our target path full string. This is where the file will be moved to
//i.e. profile_images/picture.jpg
$TARGET_PATH .= $upload_picture_fileinput['name'];
if(!is_valid_type($upload_picture_fileinput)) {
$_SESSION['error'] = "You must upload a jpeg, gif, bmp, or png";
header("Location: account.php");
exit;
}
//attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($upload_picture_fileinput['tmp_name'], $TARGET_PATH)) {
$sql = "UPDATE `users` SET `profile_image_filename`='" . $upload_picture_fileinput['name'] . "'
WHERE email='$emailstring'";
$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
header("Location: account.php");
exit;
}
else
{
$_SESSION['error'] = "Could not upload file. Check read/write permissions on the directory";
header("Location: account.php") ;
exit;
}
我正在连接数据库,图像文件名保存在我的数据库中。我只是不知道为什么上面的文件路径会打印图片,但在调整大小脚本中不起作用。如果可以的话,请帮忙。谢谢。看起来像是在$resized_image中添加了html alt标记,当然,它不应该是您输入$image->load($resized_image)的参数的一部分 尝试将此更改为
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("upload/","upload/thumbs/",100);
?>
而不是已经以html呈现为目标的字符串。看起来像是在$resized_image中添加了html alt标记,当然,该标记不应该是输入到$image->load($resized_image)中的参数的一部分 尝试将此更改为
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("upload/","upload/thumbs/",100);
?>
下面的代码创建了一个名为createThumbs的函数,该函数将获得三个参数。第一个和第二个对应地是指向包含原始图像的目录的路径,以及指向将放置缩略图的目录的路径。第三个参数是缩略图所需的宽度
下面的代码创建了一个名为createThumbs的函数,该函数将获得三个参数。第一个和第二个对应地是指向包含原始图像的目录的路径,以及指向将放置缩略图的目录的路径。第三个参数是缩略图所需的宽度
这仍然不会返回已调整大小的image@MichaelSutyak你换了哪一个男人?我也需要这样做。答案有什么变化吗?这仍然没有返回调整大小的答案image@MichaelSutyak你换了哪一个男人?我也需要这样做。答案有什么变化吗?
$image->load('profile_images/' . $row['profile_image_filename']);
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("upload/","upload/thumbs/",100);
?>