How to resize image through php script
You can try this Image Resize function tutorial
And also you can used this code for resize function(GD).
<?php
$thumb = new Imagick();
$thumb->readImage('myimage.gif'); $thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->clear();
$thumb->destroy();
?>
Or, a shorter version of the same:
<?php
$thumb = new Imagick('myimage.gif');
$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->destroy();
?>
And also refer this links for Resize image
1.YETANOTHERLinks
2.9Lession
And also converting Base64 for image Refer this Links
Image resize without saving
First of all ensure that url of your image is correct. Lets assume that you have some resize.php
for resizing your image.
resize.php
<?php
$source_image = imagecreatefromjpeg("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSKTUtEWtyngG0EVhrOOZqJYhVPoFg5rzma6Xgn6Sy-RQCDCT950g");
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = 100;
$dest_imagey = 100;
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0,
$dest_imagex,
$dest_imagey, $source_imagex, $source_imagey);
header("Content-Type: image/jpeg");
imagejpeg($dest_image,NULL,80);
?>
here I just changed URL with an address I'm sure exist
and then use it in some html
:
<img src = 'resize.php' />
Resize images in PHP without using third-party libraries?
Finally, I've discovered a way that fit my needs. The following snippet will resize an image to the specified width, automatically calculating the height in order to keep the proportion.
$image = $_FILES["image"]["tmp_name"];
$resizedDestination = $uploadDirectory.md5($randomNumber.$filename)."_RESIZED.jpg";
copy($_FILES, $resizedDestination);
$imageSize = getImageSize($image);
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];
$DESIRED_WIDTH = 100;
$proportionalHeight = round(($DESIRED_WIDTH * $imageHeight) / $imageWidth);
$originalImage = imageCreateFromJPEG($image);
$resizedImage = imageCreateTrueColor($DESIRED_WIDTH, $proportionalHeight);
imageCopyResampled($images_fin, $originalImage, 0, 0, 0, 0, $DESIRED_WIDTH+1, $proportionalHeight+1, $imageWidth, $imageHeight);
imageJPEG($resizedImage, $resizedDestination);
imageDestroy($originalImage);
imageDestroy($resizedImage);
To anyone else seeking a complete example, create two files:
<!-- send.html -->
<html>
<head>
<title>Simple File Upload</title>
</head>
<body>
<center>
<div style="margin-top:50px; padding:20px; border:1px solid #CECECE;">
Select an image.
<br/>
<br/>
<form action="receive.php" enctype="multipart/form-data" method="post">
<input type="file" name="image" size="40">
<input type="submit" value="Send">
</form>
</div>
</center>
</body>
<?php
// receive.php
$randomNumber = rand(0, 99999);
$uploadDirectory = "images/";
$filename = basename($_FILES['file_contents']['name']);
$destination = $uploadDirectory.md5($randomNumber.$filename).".jpg";
echo "File path:".$filePath."<br/>";
if (is_uploaded_file($_FILES["image"]["tmp_name"]) == true) {
echo "File successfully received through HTTP POST.<br/>";
// Validate the file size, accept files under 5 MB (~5e+6 bytes).
if ($_FILES['image']['size'] <= 5000000) {
echo "File size: ".$_FILES["image"]["size"]." bytes.<br/>";
// Resize and save the image.
$image = $_FILES["image"]["tmp_name"];
$resizedDestination = $uploadDirectory.md5($randomNumber.$filename)."_RESIZED.jpg";
copy($_FILES, $resizedDestination);
$imageSize = getImageSize($image);
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];
$DESIRED_WIDTH = 100;
$proportionalHeight = round(($DESIRED_WIDTH * $imageHeight) / $imageWidth);
$originalImage = imageCreateFromJPEG($image);
$resizedImage = imageCreateTrueColor($DESIRED_WIDTH, $proportionalHeight);
imageCopyResampled($images_fin, $originalImage, 0, 0, 0, 0, $DESIRED_WIDTH+1, $proportionalHeight+1, $imageWidth, $imageHeight);
imageJPEG($resizedImage, $resizedDestination);
imageDestroy($originalImage);
imageDestroy($resizedImage);
// Save the original image.
if (move_uploaded_file($_FILES['image']['tmp_name'], $destination) == true) {
echo "Copied the original file to the specified destination.<br/>";
}
}
}
?>
Resize images with PHP, support PNG, JPG
function resize($newWidth, $targetFile, $originalFile) {
$info = getimagesize($originalFile);
$mime = $info['mime'];
switch ($mime) {
case 'image/jpeg':
$image_create_func = 'imagecreatefromjpeg';
$image_save_func = 'imagejpeg';
$new_image_ext = 'jpg';
break;
case 'image/png':
$image_create_func = 'imagecreatefrompng';
$image_save_func = 'imagepng';
$new_image_ext = 'png';
break;
case 'image/gif':
$image_create_func = 'imagecreatefromgif';
$image_save_func = 'imagegif';
$new_image_ext = 'gif';
break;
default:
throw new Exception('Unknown image type.');
}
$img = $image_create_func($originalFile);
list($width, $height) = getimagesize($originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
$image_save_func($tmp, "$targetFile.$new_image_ext");
}
Resize image on upload
You should try using GD.
// Loads the image
$img = imagecreatefromjpeg($_FILES["immagine"]["tmp_name"]); // Assuming it's a jpeg
// Creates a image to put the thumbnail
$tmp_img = imagecreatetruecolor(<thumbnail width>, <thumbnail height>)
// Resizes the image
imagecopyresampled ( $tmp_img, $img, 0, 0, 0, 0, <thumbnail width>, <thumbnail height>, <original width>, <original height>); // The zeroes are the offsets
// Starts output buffer to not let the image reach the browser
ob_start();
// Render the image
imagejpeg($tmp_image);
// Capture the image data
$imageData = ob_get_clean();
PHP image resize not working
The function getimagesize()
requires a filename, not an image resource. It emits a warning, that's why the image was not valid, because there is a string before the image binary.
$photos = glob($_GET['a'] . '/*.*');
$filename = $photos[array_rand($photos)] ;
$img = imagecreatefromjpeg($filename);
list($width, $height) = getimagesize($filename);
or use imagesx()
and imagesy()
to get image size using $img
.
$photos = glob($_GET['a'] . '/*.*');
$img = imagecreatefromjpeg($photos[array_rand($photos)]);
$width = imagesx($img);
$height = imagesy($img);
Related Topics
How to Make a Request Using Http Basic Authentication With PHP Curl
Get $_Post from Multiple Checkboxes
How to Retrieve Images from MySQL Database and Display in an HTML Tag
Should I Manually Check For Errors When Calling "MySQLi_Stmt_Prepare"
Difference Between Http_Host and Server_Name in PHP
Pass a PHP Array to a JavaScript Function
How to Access PHP Variables in JavaScript or Jquery Rather Than ≪PHP Echo $Variable ≫
Fatal Error: Uncaught Error: Call to Undefined Function MySQL_Connect()
How to Get the Jquery $.Ajax Error Response Text
How Can One Use Multi Threading in PHP Applications
Best Way to Use PHP to Encrypt and Decrypt Passwords
Apache Is Downloading PHP Files Instead of Displaying Them
Remove a Child With a Specific Attribute, in Simplexml For PHP
How to Encrypt/Decrypt Data in PHP