function resize_pic($img, $x, $y)
{
    $img = urldecode($img);
    if ($img && strlen($img) > 5) {
        $final_ext;
        $img_old;
        if (preg_match("/\\.jpg|\\.jpeg/i", $img)) {
            $img_old = imagecreatefromjpeg($img);
            $final_ext = ".jpg";
            $t = 1;
        } else {
            if (stristr($img, ".gif")) {
                $img_old = imagecreatefromgif($img);
                $final_ext = ".gif";
                $t = 2;
            } else {
                if (stristr($img, ".png")) {
                    $img_old = imagecreatefrompng($img);
                    $final_ext = ".png";
                    $t = 3;
                } else {
                    $img_old = false;
                    $t = false;
                }
            }
        }
        if ($img_old && $t) {
            list($xx, $yy) = getimagesize($img);
            $newx;
            $newy;
            if ($xx < $x || $yy < $y) {
                header("location: {$img}");
                exit;
            }
            if ($xx < $yy) {
                $newx = $x;
                $newy = intval($yy / $xx * $x);
            } else {
                $newy = $y;
                $newx = intval($xx / $yy * $y);
            }
            try {
                $new_img = imagecreatetruecolor($newx, $newy);
                if ($final_ext == ".gif" || $final_ext == ".png") {
                    imagecolortransparent($new_img, imagecolorallocatealpha($new_img, 0, 0, 0, 127));
                    imagealphablending($new_img, false);
                    imagesavealpha($new_img, true);
                }
                if (!$new_img) {
                }
                //throw new Exception("could not save");
                imagecopyresampled($new_img, $img_old, 0, 0, 0, 0, $newx, $newy, $xx, $yy);
            } catch (Exception $err) {
                echo $err->getMessage();
                return false;
            }
            switch ($t) {
                case 1:
                    headerCache();
                    header("Content-type:image/jpg");
                    imagejpeg($new_img);
                    break;
                case 2:
                    headerCache();
                    header("Content-type:image/gif");
                    imagegif($new_img);
                    break;
                case 3:
                    headerCache();
                    header("Content-type:image/png");
                    imagepng($new_img);
                    break;
                default:
                    header("location: {$img}");
                    break;
            }
            exit;
        } else {
            header("location: {$img}");
        }
    } else {
        header("location: {$img}");
    }
}
Example #2
0
 protected function _newFileData($list)
 {
     if ($list && is_array($list)) {
         //缓存图片开始
         headerCache();
         //缓存图片结束
         foreach ($list as $key => $value) {
             $id = $value['id'];
             //缓存上传的图片(缩放后的图片)
             $cache_dir = '/temp/photo/' . $value['uid'] . '/' . $value['type_id'] . '/' . $value['dir_id'] . '/';
             $file_path = $cache_dir . md5($value['upload_path']) . '.' . $value['file_type'];
             if (!is_file(ROOT_PATH . $file_path)) {
                 PhotoM::cachePhoto($cache_dir, $value['upload_path'], $value['file_type']);
             }
             $list[$key]['upload_path'] = $file_path;
             $list[$key]['del_file_url'] = url("myweb", "photo::del", "dfile_" . $value['dir_id'] . "_" . $id);
         }
     }
     return $list;
 }