[英]How to resize the image in php
My Updated code /*$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); $path = $_FILES['file']['tmp_name']; $filename = file_get_contents($path);*/ /* $imgdata = base64_encode($im); // here we got base64 encode value $idproof = array("encode" => $imgdata, "path" =>$path, "extension" =>$extension, ); echo json_encode($idproof);*/ $image; $image_type; $width = 150; $height = 150; //----------------------------------------------- // Load Image file //----------------------------------------------- function load($filename) { global $image; global $image_type; $image_info = getimagesize($filename); $image_type = $image_info[2]; if($image_type == IMAGETYPE_JPEG) { $image = imagecreatefromjpeg($filename); } elseif($image_type == IMAGETYPE_GIF) { $image = imagecreatefromgif($filename); } elseif($image_type == IMAGETYPE_PNG) { $image = imagecreatefrompng($filename); } } //----------------------------------------------- // Save Image file //----------------------------------------------- function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { global $image; global $image_type; if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($image,$filename); } if( $permissions != null ) { chmod($filename,$permissions); } } //----------------------------------------------- // View Image file //----------------------------------------------- function output($image_type=IMAGETYPE_JPEG) { global $image; global $image_type; if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($image); } } function getWidth() { global $image; return imagesx($image); } function getHeight() { global $image; return imagesy($image); } //============================================================================= // Resize Images //============================================================================= //----------------------------------------------- // Resize Images 01 - Resize to Height //----------------------------------------------- function resizeToHeight($height) { $ratio = $height / getHeight(); $width = getWidth() * $ratio; resize($width,$height); } //----------------------------------------------- // Resize Images 02 - Resize to Width //----------------------------------------------- function resizeToWidth($width) { $ratio = $width / getWidth(); $height = getheight() * $ratio; resize($width,$height); } //----------------------------------------------- // Resize Images 03 - Scale in % //----------------------------------------------- function scale($scale) { $width = getWidth() * $scale/100; $height = getheight() * $scale/100; resize($width,$height); } //----------------------------------------------- // Resize Images 04 - Scale in % //----------------------------------------------- function resize($width,$height) { global $image; $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, getWidth(), getHeight()); $image = $new_image; } load($_FILES['file']['tmp_name']); // To resize based on image width resizeToWidth($width); // To resize based on image height resizeToHeight($height); // To resize to specific size resize($width, $height) ; $filename = base64_encode(output()); $idproof = array("encode" => $filename, ); echo json_encode($idproof);
I am doing one php file upload,in that field user uploads the large image means, i want to re size the image, see here i want like this ,but this code i can't understand,next after re size the image i want to encode that image how can do this one? 我正在上传一个php文件,在该字段中,用户上传了大图像,我想重新调整图像大小, 请参阅此处,我想要这样,但是这段代码我听不懂,接下来重新调整我想要的图像大小对该图像进行编码怎么办?
<script type="text/javascript"> $("#idproof").submit(function(e) { var formData = new FormData(); var id = '<?php echo $id;?>'; formData.append('ssmid', id); formData.append('file', $('input[type=file]')[0].files[0]); $.ajax({ url: 'idproof_check.php', type: 'POST', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (data) { var res=jQuery.parseJSON(data);// convert the json console.log(res); }, }); }); </script> idproof_check.php <?php $userid = $_POST['userid']; $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); $path = $_FILES['file']['tmp_name']; $im = file_get_contents($path); /*Here i want to resize the image after that i want to encode the image*/ $filename = base64_encode($im); // here we got base64 encoded value $idproof = array("encode" => $filename, "path" =>$path, "extension" =>$extension, ); echo json_encode($idproof); ?>
<form method="post" enctype="multipart/form-data" class="form-horizontal" id="idproof" name="myForm"> <div class="input-group heading1"> <span class="input-group-btn"> <span class="btn btn-primary btn-file"> Browse… <input type="file" id="myFile" name="file" > </span> </span> <input type="text" class="form-control" readonly=""> </div> <div class="row"> <div class="col-md-5 col-md-offset-2"> <div class="input-group heading1"> <span class="input-group-btn"> <button type="submit" class="btn btn-primary btn-md horoscope">Upload ID Proof</button> </div> </div> </div> </form>
Here's the php class to resize images. 这是用于调整图像大小的php类。
<?php
/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/
$image;
$image_type;
//-----------------------------------------------
// Load Image file
//-----------------------------------------------
function load($filename)
{
global $image;
global $image_type;
$image_info = getimagesize($filename);
$image_type = $image_info[2];
if($image_type == IMAGETYPE_JPEG)
{
$image = imagecreatefromjpeg($filename);
}
elseif($image_type == IMAGETYPE_GIF)
{
$image = imagecreatefromgif($filename);
}
elseif($image_type == IMAGETYPE_PNG)
{
$image = imagecreatefrompng($filename);
}
}
//-----------------------------------------------
// Save Image file
//-----------------------------------------------
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null)
{
global $image;
global $image_type;
if( $image_type == IMAGETYPE_JPEG )
{
imagejpeg($image,$filename,$compression);
}
elseif( $image_type == IMAGETYPE_GIF )
{
imagegif($image,$filename);
}
elseif( $image_type == IMAGETYPE_PNG )
{
imagepng($image,$filename);
}
if( $permissions != null )
{
chmod($filename,$permissions);
}
}
//-----------------------------------------------
// View Image file
//-----------------------------------------------
function output($image_type=IMAGETYPE_JPEG)
{
global $image;
global $image_type;
if( $image_type == IMAGETYPE_JPEG )
{
imagejpeg($image);
}
elseif( $image_type == IMAGETYPE_GIF )
{
imagegif($image);
} elseif( $image_type == IMAGETYPE_PNG )
{
imagepng($image);
}
}
function getWidth()
{
global $image;
return imagesx($image);
}
function getHeight()
{
global $image;
return imagesy($image);
}
//=============================================================================
// Resize Images
//=============================================================================
//-----------------------------------------------
// Resize Images 01 - Resize to Height
//-----------------------------------------------
function resizeToHeight($height)
{
$ratio = $height / getHeight();
$width = getWidth() * $ratio;
resize($width,$height);
}
//-----------------------------------------------
// Resize Images 02 - Resize to Width
//-----------------------------------------------
function resizeToWidth($width)
{
$ratio = $width / getWidth();
$height = getheight() * $ratio;
resize($width,$height);
}
//-----------------------------------------------
// Resize Images 03 - Scale in %
//-----------------------------------------------
function scale($scale)
{
$width = getWidth() * $scale/100;
$height = getheight() * $scale/100;
resize($width,$height);
}
//-----------------------------------------------
// Resize Images 04 - Scale in %
//-----------------------------------------------
function resize($width,$height)
{
global $image;
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, getWidth(), getHeight());
$image = $new_image;
}
?>
Call this class using: 使用以下方法调用此类:
load($_FILES["IMAGE_FILE"]["tmp_name"]);
// To resize based on image width
resizeToWidth($width);
// To resize based on image height
resizeToHeight($height);
// To resize to specific size
resize($width, $height)
// To save file in web server directory
save($PATH);
// To retrieve resized images directly from variable
output();
EDIT: You don't need echo
to make a function call. 编辑:您不需要
echo
即可进行函数调用。
$width = 150;
$height = 150;
load($_FILES['file']['tmp_name']);
// To resize to specific size
resize($width, $height)
$filename = base64_encode(output());
Call this function where you insert your image 在插入图像的地方调用此函数
<?php
function make_thumb($src, $dest, $desired_width, $desired_height) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/*if($width <600){
$desired_width = $width;
}*/
/* find the "desired height" of this thumbnail, relative to the desired width */
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
}
$src = "../../gallery_image/$file_name";
$dest = "../../gallery_image/thumb/$file_name";
$desired_width = "360";
$desired_height = "260";
make_thumb($src, $dest, $desired_width, $desired_height);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.