使用gd上传

时间:2017-03-10 20:57:54

标签: php gd

我正在尝试在上传时调整图片大小,但我收到此错误。 警告:imagecreatefromjpeg(myimage.jpg):无法打开流:没有这样的文件或目录。为什么我的代码不起作用?我能做些什么让它发挥作用?

//Input values
$image = htmlspecialchars($_FILES['image']['name']);

// image file type
$uploadedfile = $_FILES['image']['name'];
$imageFileType = pathinfo($image, PATHINFO_EXTENSION);

switch ($imageFileType) {
    case 'jpg':
        $image_create = 'imagecreatefromjpeg';
        $image_save = 'imagejpeg';
        $new_image_ext = 'jpg';
        break;
    case 'jpeg':
        $image_create = 'imagecreatefromjpeg';
        $image_save = 'imagejpeg';
        $new_image_ext = 'jpeg';
        break;
    case 'png':
        $image_create = 'imagecreatefrompng';
        $image_save = 'imagepng';
        $new_image_ext = 'png';
        break;
    default:
        echo "use a image";
}

$img_format = $image_create($image);
list($width, $height) = getimagesize($image);

// new height and width
$newHeight = 150;
$newWidth = 150;

// create new image with new width and height
$destination = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($destination, $img_format, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
    echo "Sorry, only JPG, JPEG, PNG files are allowed.";
} else {
    if($image_save($destination, "images/".$uploadedfile.".".$new_image_ext, 100)){
        $sql = "UPDATE home SET image='$image' WHERE images_id=1";
        echo "Succes";
    } else{
        echo "Failed";
    }
}

0 个答案:

没有答案