Use php to cut the pictures that are uploaded to the database.

51 5
//获取上传临时文件路径
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
//获取源文件资源句柄。接收参数为图片路径,返回句柄
$source = ImageCreateFromJPEG($uploadedfile);
//获取文件的长宽
list($width,$height) = GetImageSize($uploadedfile);
if ($width> $height) {
 $xoord = ceil(($width-$height)/2);
 $width = $height;
} else {
 $yoord = ceil(($height-$width)/2);
 $height = $width;
}
//创建一个图片。接收参数分别为宽高,返回生成的资源句柄
$thumb = ImageCreatetruecolor(100,100);//大图
//将源文件剪切全部域并缩小放到目标图片上。前两个为资源句柄 
ImageCopyResampled($thumb,$source,0,0,$xoord,$yoord,100,100,$width,$height);
//图片显示
ImageJPEG($thumb,'',100);

It's the process of image cutting, assuming that I can operate the database directly through $ db-> select | update | insert. I'm going to.

1 Answers

140 2

Although someone is advised that you don't exist in the database, I'm still telling you if you stick to it. First, the fields you want to save are blob format.

//从最后一行开始, 用一个临时文件做中转
$tmp = '/tmp/'. md5(uniqid());
ImageJPEG($thumb, $tmp, 100);
//把数据转换成一个字符串
$str = mysql_real_escape_string(file_get_contents($tmp));
//这只是一个测试的sql,你可以自己修改
$sql = sprintf("INSERT INTO test (`blob_column`) VALUES ('%s')", $str);
mysql_query($sql);
//别忘了把临时文件删掉
unlink($tmp);
...