php 调整图像流中图像的大小
php resizing image from image stream
基本上,图像是在 canvas html5 元素上创建的,并使用以下代码在服务器上保存为图像
<?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;
}
}
?>
问题已解决:
代码现在可以工作了。图像类型应该是整个 mime 类型 - image/png 而不仅仅是 png
这是固定代码,希望对大家有帮助:
<?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, "image/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;
}
}
?>
基本上,图像是在 canvas html5 元素上创建的,并使用以下代码在服务器上保存为图像
<?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;
}
}
?>
问题已解决:
代码现在可以工作了。图像类型应该是整个 mime 类型 - image/png 而不仅仅是 png
这是固定代码,希望对大家有帮助:
<?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, "image/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;
}
}
?>