h5图片 拍照 iphone 旋转 翻转 base64图片 保存

function getThumb($sFile,$iWidth,$iHeight){

$ioscam = $_POST['ioscam'];
if($ioscam == '1'){
// if(!defined('CLASS_IMAGICK')){require(Inc.'class_imagick.php');}
// $imagick = new Imagick();
// $imagick->readImage($sFile);
// $imagick->flopImage();
// $imagick->writeImage($sFile);
// $imagick->clear();
// $imagick->destroy();
$back =imagecreatefromjpeg($sFile);

$width = imagesx($back);
$height = imagesy($back);

//创建一个新的图片资源,用来保存沿Y轴翻转后的图片
$new =imagecreatetruecolor($width, $height);
//沿y轴翻转就是将原图从右向左按一个像素宽度向新资源中逐个复制
for($x=0 ;$x<$width; $x++){
//逐条复制图片本身高度,1个像素宽度的图片到新资源中
imagecopy($new,$back, $width-$x-1, 0, $x, 0, 1, $height);
}
//保存翻转后的图片
imagejpeg($new,$sFile);
imagedestroy($back);
imagedestroy($new);

}

 


//判断该图片是否存在
if(!file_exists($sFile)) return $sFile;
//判断图片格式
$attach_fileext = get_filetype($sFile);
if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
return $sFile;
}
//压缩图片
$sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);

//判断是否已压缩图片,若是则返回压缩图片路径
if(file_exists($sFileNameS)){
return $sFileNameS;
}
//解决手机端上传图片被旋转问题
if (in_array($attach_fileext, array('jpeg')) ){
adjustPicOrientation($sFile);
}
//生成压缩图片,并存储到原图同路径下
resizeImage($sFile, $sFileNameS, $iWidth, $iHeight);
if(!file_exists($sFileNameS)){
return $sFile;
}
return $sFileNameS;
}

/**
*获取文件后缀名
*/
function get_filetype($filename) {
$extend = explode("." , $filename);
return strtolower($extend[count($extend) - 1]);
}

/**
* 解决手机上传图片被旋转问题
* @param string $full_filename 文件路径
*/
function adjustPicOrientation($full_filename){
$exif = exif_read_data($full_filename);
if($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if($orientation != 1){
$img = imagecreatefromjpeg($full_filename);

$mirror = false;
$deg = 0;

switch ($orientation) {
case 2:
$mirror = true;
break;
case 3:
$deg = 180;
break;
case 4:
$deg = 180;
$mirror = true;
break;
case 5:
$deg = 270;
$mirror = true;
break;
case 6:
$deg = 270;
break;
case 7:
$deg = 90;
$mirror = true;
break;
case 8:
$deg = 90;
break;
}
if ($deg) $img = imagerotate($img, $deg, 0);
if ($mirror) $img = _mirrorImage($img);
//$full_filename = str_replace('.jpg', "-O$orientation.jpg", $full_filename);新文件名
imagejpeg($img, $full_filename, 95);
}
}
return $full_filename;
}


/**
* 生成图片
* @param string $im 源图片路径
* @param string $dest 目标图片路径
* @param int $maxwidth 生成图片宽
* @param int $maxheight 生成图片高
*/
function resizeImage($im, $dest, $maxwidth, $maxheight) {
$img = getimagesize($im);
switch ($img[2]) {
case 1:
$im = @imagecreatefromgif($im);
break;
case 2:
$im = @imagecreatefromjpeg($im);
break;
case 3:
$im = @imagecreatefrompng($im);
break;
}

$pic_width = imagesx($im);
$pic_height = imagesy($im);
$resizewidth_tag = false;
$resizeheight_tag = false;
if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
if ($maxwidth && $pic_width > $maxwidth) {
$widthratio = $maxwidth / $pic_width;
$resizewidth_tag = true;
}

if ($maxheight && $pic_height > $maxheight) {
$heightratio = $maxheight / $pic_height;
$resizeheight_tag = true;
}

if ($resizewidth_tag && $resizeheight_tag) {
if ($widthratio < $heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}


if ($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if ($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;

if (function_exists("imagecopyresampled")) {
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
} else {
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
}

imagejpeg($newim, $dest);
imagedestroy($newim);
} else {
imagejpeg($im, $dest);
}
}


function base64_image_content($base64_image_content){
//匹配出图片的格式
$new_file ="rhpic/".time().".png";
if (file_put_contents($new_file, base64_decode($base64_image_content))){
return $new_file;
}else{
return false;
}

}

posted on 2018-07-16 14:51  一切都要简单化  阅读(1036)  评论(0编辑  收藏  举报

导航