$picFile = $_FILES["picFileName"];
$picType = $picFile["type"];
$picData = file_get_contents($tempFile);
$tempFile = $picFile["tmp_name"];
$picData = base64_encode($picData);
$query = "INSERT INTO image
(imgid, image, imgtype)
values(
null,
'$picData',
'$picType'
);";
$link->query($query);
list($picW, $picH) = getimagesize($tempFile);
if($picW>600){ //超出大小
$src = imagecreatefromjpeg($tempFile); //获取原图数据
$nW =550;
$nH = $picH*$nW/$picW;
$newPicTemp = imagecreatetruecolor($nW,$nH); //创建彩色图片对象
imagecopyresampled($newPicTemp,$src,0,0,0,0,$nW,$nH,$picW,$picH);
}
string ob_get_contents ( void )
ob_start(); //开启输出缓冲区
imagejpeg($newPicTemp); //这个函数可以显示出图片,同时也是把数据输出
$imgContent = ob_get_contents(); //获取字符流
ob_end_clean(); //关闭并清除缓冲区
$picData = base64_encode($imgContent); //不要忘记转码
imagedestroy($src);
imagedestroy($newPicTemp);
$query = "SELECT image, imgtype from image where imgid = $imgid ;";
$result = $link->query($query);
$row = mysqli_fetch_array($result);
$data = base64_decode( $row["image"]);
$type = $row["imgtype"];
ob_clean();
header("Content-Type:'$type'");
echo $data;
以上就介紹了PHP裁剪圖片並存入mysql,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。