我的代码只上传图片,但没有调整图片的大小(宽度/高度)。
我尝试了this解决方案并将其嵌入,但它无效。
任何人都可以为我找到解决方案。
这是我原来的代码
文件名: avatar.php
<?php
// declare variables
$msg = '';
$f_avatar_image = '';
// ------------------------------------------------------------
// UPLOAD AVATAR
// ------------------------------------------------------------
if(isset($_POST['btnUploadAvatar']) && !empty($_FILES['fileUpload']['name']))
{
// create variables
$avatar_directory = AVATAR_FILE_DIRECTORY;
$file_name = $_FILES['fileUpload']['name'];
$file_type = $_FILES['fileUpload']['type'];
$file_size = $_FILES['fileUpload']['size'];
$file_size_limit = AVATAR_FILE_SIZE;
$calc_kilobites = 1024;
$file_size_kb = round($file_size / $calc_kilobites, 2);
$temp_file_name = $_FILES['fileUpload']['tmp_name'];
$upload_error = $_FILES['fileUpload']['error'];
// create unique file name
$unique_file_name = $user_name.'-'.$file_name;
$avatar_img_url = AVATAR_IMAGE_URL.$user_name.'-'.$file_name;
// if upload error display error message
if($upload_error > 0)
{
echo 'ERROR:' . $upload_error;
}
// if no upload error - check for file types
if($upload_error == 0 &&
$file_type == 'image/gif' ||
$file_type == 'image/jpeg' ||
$file_type == 'image/png' )
{
// if file size is within limits
if($file_size <= $file_size_limit)
{
// move uploaded file to assigned directory
if(move_uploaded_file($temp_file_name, $avatar_directory . $unique_file_name))
{
// get user id
$get_user_id = mysqli_query($conn, "SELECT UserId FROM users WHERE UserName = '$user_name' Limit 1") or die($dataaccess_error);
// if user id exist
if(mysqli_num_rows($get_user_id) == 1 )
{
$row = mysqli_fetch_array($get_user_id);
$user_id = $row['UserId'];
// check if user profile already exist
$check_user_profile = mysqli_query($conn, "SELECT UserId FROM profiles WHERE UserName = '$user_name' Limit 1") or die($dataaccess_error);
// if user profile exist - update
if(mysqli_num_rows($check_user_profile) == 1 )
{
// update profiles
$update_profile = mysqli_query($conn, "UPDATE profiles SET AvatarImage = '$avatar_img_url' WHERE UserName = '$user_name'") or die($dataaccess_error);
if(mysqli_affected_rows($conn) > 0)
{
echo 'Upload Success! <br/>';
echo 'File Name: '.$file_name.'<br/>';
echo 'File Type: '.$file_type.'<br/>';
echo 'File Size: '.$file_size_kb.' Kb <br/>';
$msg = $profile_update_success;
}
else
{
$msg = $profile_update_failed;
}
}
else
{
// create profile
$insert_profile = mysqli_query($conn, "INSERT INTO profiles(UserId,UserName,AvatarImage) VALUES($user_id,'$user_name','$avatar_img_url')") or die($dataaccess_error);
if(mysqli_affected_rows($conn) > 0)
{
echo 'Upload Success! <br/>';
echo 'File Name: '.$file_name.'<br/>';
echo 'File Type: '.$file_type.'<br/>';
echo 'File Size: '.$file_size_kb.' Kb <br/>';
$msg = $profile_update_success;
}
else
{
$msg = $profile_create_failed;
}
}
}
else
{
// user id not found
$msg = $profile_update_failed2;
}
}
else
{
$msg = $avatar_upload_failed;
}
}
else
{
$msg = $avatar_file_too_large;
}
}
else
{
$msg = $avatar_wrong_file_type;
}
}
elseif(isset($_POST['btnUploadAvatar']) && empty($_FILES['fileUpload']['name']))
{
$msg = $avatar_empty;
}
// ------------------------------------------------------------
// DISPLAY AVATAR ON PAGE LOAD
// ------------------------------------------------------------
if($user_name)
{
// get user id
$get_avatar_image = mysqli_query($conn, "SELECT AvatarImage FROM profiles WHERE UserName = '$user_name' Limit 1") or die($dataaccess_error);
if(mysqli_num_rows($get_avatar_image) == 1)
{
$row = mysqli_fetch_array($get_avatar_image);
if($row['AvatarImage'] != 'NULL' && $row['AvatarImage'] != '')
{
$f_avatar_image = $row['AvatarImage'];
}
else
{
$f_avatar_image = AVATAR_IMAGE_URL.DEFAULT_AVATAR_IMAGE;
}
}
else
{
$f_avatar_image = AVATAR_IMAGE_URL.DEFAULT_AVATAR_IMAGE;
}
}
?>
这是我要嵌入它的代码
error_reporting(0);
$change="";
$abc="";
define ("MAX_SIZE","400");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
$change='<div class="msgdiv">Unknown Image extension </div> ';
$errors=1;
}
else
{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
$newwidth=150;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=50;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$filename = "profile.pic/profile/big" .date('Y-m-d_His - '). $_FILES['file']['name'];
$filename1 = "profile.pic/header/small".date('Y-m-d_His - '). $_FILES['file']['name'];
// mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'");
$change=' Image Uploaded Successfully!';
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}
}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
// mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'");
$change=' <div class="msgdiv">Image Uploaded Successfully!</div>';
}
答案 0 :(得分:0)
很简单。从上面的代码中我们将得到一个变量中的图像URL,我们可以在out resize函数中使用它,如下所示
$ imgUrl =&#39; http://www.planet-source-code.com/vb/2010Redesign/images/LangugeHomePages/PHP.png&#39 ;;
现在在html中只需制作以下内容
&#34;高度=&#34; 50&#34;宽度=&#34; 50&#34; /&GT;
希望这会有所帮助。
答案 1 :(得分:0)
Imagemagick一直对我很有帮助!例如,从sitepoint:
$inFile = "test.jpg";
$outFile = "test-thumbnail.jpg";
$image = new Imagick($inFile);
$image->thumbnailImage(200, 200);
$image->writeImage($outFile);
这是一个非常好的教程:http://www.sitepoint.com/crop-and-resize-images-with-imagemagick/
希望有所帮助!