0
基本上是一個圖像是畫布HTML5元素上創建並使用下面的代碼PHP調整圖像
<?php
$upload_dir = "uploads/";
$img = $_POST['hidden_data'];
$imageID = $_POST['imageID'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = $upload_dir . "gillette_" . $imageID . ".png";
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
我想保存縮略圖版本100x100像素,但在服務器上保存爲圖像更喜歡做直接從流中。任何幫助將不勝感激
下面的代碼調整和保存圖像沒有工作,試圖用imagecreatefromstring
<?php
############ Configuration ##############
$thumb_square_size = 100; //Thumbnails will be cropped to 200x200 pixels
$max_image_size = 520; //Maximum image size (height and width)
$thumb_prefix = "small_"; //Normal thumb Prefix
$destination_folder = 'uploads/'; //upload directory ends with/(slash)
$jpeg_quality = 90; //jpeg quality
##########################################
$upload_dir = "uploads/";
$img = $_POST['hidden_data'];
$imageID = $_POST['imageID'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = $upload_dir . "gillette_" . $imageID . ".png";
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
$im = imagecreatefromstring($data);
if($im){
$new_file_name = $imageID . ".png";
$thumb_save_folder = $destination_folder . $thumb_prefix . $new_file_name;
//call crop_image_square() function to create square thumbnails
if(!crop_image_square($im, $thumb_save_folder, "png", $thumb_square_size, 520, 382, $jpeg_quality))
{
die('Error Creating thumbnail');
}
imagedestroy($im); //freeup memory
}
##### This function corps image to create exact square, no matter what its original size! ######
function crop_image_square($source, $destination, $image_type, $square_size, $image_width, $image_height, $quality){
if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize
if($image_width > $image_height)
{
$y_offset = 0;
$x_offset = ($image_width - $image_height)/2;
$s_size = $image_width - ($x_offset * 2);
}else{
$x_offset = 0;
$y_offset = ($image_height - $image_width)/2;
$s_size = $image_height - ($y_offset * 2);
}
$new_canvas = imagecreatetruecolor($square_size, $square_size); //Create a new true color image
//Copy and resize part of an image with resampling
if(imagecopyresampled($new_canvas, $source, 0, 0, $x_offset, $y_offset, $square_size, $square_size, $s_size, $s_size)){
save_image($new_canvas, $destination, $image_type, $quality);
}
return true;
}
##### Saves image resource to file #####
function save_image($source, $destination, $image_type, $quality){
switch(strtolower($image_type)){//determine mime type
case 'image/png':
imagepng($source, $destination); return true; //save png file
break;
case 'image/gif':
imagegif($source, $destination); return true; //save gif file
break;
case 'image/jpeg': case 'image/pjpeg':
imagejpeg($source, $destination, $quality); return true; //save jpeg file
break;
default: return false;
}
}
?>
[imagecreatefromstring(HTTP://www.php .net/imagecreatefromstring)應該使用'$ data'。創建圖像資源後,任何裁剪/調整大小縮略圖腳本應該工作。 – 2015-01-20 20:09:14
嘗試從字符串實現imagecreate,但它不起作用。我添加了可以在同一個系統上傳的圖片上正常工作的新代碼 – user2251695 2015-01-21 07:42:25