I'm uploading images with HTML and PHP.
<form action="" method="post">
<input type="file" name="image" id="image">
</form>
How would I use imagemagick to resize the image if it is larger than 1500(width)x700(height) whichever is larger comes first, then reside the image.
As far as I looked for, imagemagick can only resize images after upload. Is it possible to resize images while upload and then store into directory/folder?
You can resize the temp file then save the file after it is completed.
Here is how I typically handle it.. PLEASE NOTE YOU NEED TO DO A LOT MORE WITH SECURING THIS UP! Make sure you are checking the upload allowed type, size ect..
I use this function to resize..
function img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w,
dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
Then I call the function with the temp file..
$fileName = $_FILES["image"]["name"]; // The file name
$target_file = $_FILES["image"]["tmp_name"];
$kaboom = explode(".", $fileName); // Split file name into an array using the dot
$fileExt = end($kaboom); // Now target the last array element to get the file extension
$fname = $kaboom[0];
$exten = strtolower($fileExt);
$resized_file = "uploads/newimagename.ext"; //need to change this make sure you set the extension and file name correct.. you will want to secure things up way more than this too..
$wmax = 1500;
$hmax = 700;
img_resize($target_file, $resized_file, $wmax, $hmax, $exten);
Related
I'm trying to automatically resize the image while uploading by using this function:
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
and then :
$target_file = "img/".$admit_roll.".".$ext;
$resized_file = "img/".$admit_roll.".".$ext;
$wmax = 200;
$hmax = 200;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $ext);
But this isn't returning a square sized-image even when $wmax abd $hmax are same. How should make it return a square size image ?
You could use the function imagecopyresized() to resize it:
function img_resize($target, $newcopy, $w, $h, $ext){
list($w_orig, $h_orig) = getimagesize($target);
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
imagecopyresized($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy);
}
With this function you don't have to care about aspect ratio, the function does for you.
Edit: You can replace the function imagecopyresized()with imagecopyresampled(), it will do the same thing, although with more quality, but using more cpu time
Image manipulation is complex. You can learn to do it yourself, that's fun and sometimes useful. But it also tedious as you need to learn all the problems that others already solved.
Another alternative is to use one of the libraries that make it easy for you to achieve this.
For example: Intervention Image
Take a look how its easier to fit image into a frame.
// open file a image resource
$img = Image::make('public/foo.jpg');
// crop the best fitting 5:3 (600x360) ratio and resize to 600x360 pixel
$img->fit(600, 360);
// crop the best fitting 1:1 ratio (200x200) and resize to 200x200 pixel
$img->fit(200);
// add callback functionality to retain maximal original image size
$img->fit(800, 600, function ($constraint) {
$constraint->upsize();
});
I writing code that uploads an image from a url but I need to change the dimensions of the image to 250px * 250px when it's uploading.
How can I change my code to do that?
Example : If I upload an image of 130px * 185px dimensions, it's saving in my upload dir at 250px *250px automatically.
The code that I use is:
<?php
/* photo uploader*/
if($_POST["submit"]){
$pic = trim($_POST["pic_url"]);
if($pic){
$photo = fopen($pic,"rb");
if($photo){
$valid_exts = array("jpg","jpeg","gif","png","Bmp","TIFF"); // default image only extensions
if($valid_exts){
$newpic = fopen("../../cat/adventure/images/" . basename($pic), "wb"); // replace "downloads" with whatever directory you wish.
if($newpic){
while(!feof($photo)){
// Write the url file to the directory.
fwrite($newpic,fread($photo,1024 * 8),1024 * 8); // write the file to the new directory at a rate of 8kb/sec. until we reach the end.
}
}
}
}
}
}
?>
You can use the following universal function:
include_once("ak_php_img_lib_1.0.php");
$target_file = "uploads/$fileName";
$resized_file = "uploads/resized_$fileName";
$wmax = 200;
$hmax = 150;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
<?php
// Function for resizing jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
I am currently having problem re-sizing images in upload form of php.
Here is my code :
<?php
// Function for resizing jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
<?php
if (isset($_FILES['profilepic'])) {
if (((#$_FILES["profilepic"]["type"]=="image/jpeg") || (#$_FILES["profilepic"]["type"]=="image/png") || (#$_FILES["profilepic"]["type"]=="image/gif"))&&(#$_FILES["profilepic"]["size"] < 1048576)) //1 Megabyte
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
$dir = "/home/rahulkapoor90/public_html/userdata/profile_pics/$rand_dir_name";
mkdir($dir);
move_uploaded_file(#$_FILES["profilepic"]["tmp_name"],"/home/rahulkapoor90/public_html/userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);
$profile_pic_name = #$_FILES["profilepic"]["name"];
$target_file = "http://www.hootpile.com/userdata/profile_pics/$rand_dir_name/$profile_pic_name";
$resized_file = "http://www.hootpile.com/userdata/profile_pics/$rand_dir_name/resized_$profile_pic_name";
$wmax = 200;
$hmax = 150;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $imageFileType);
$profile_pic_query = mysqli_query($conn,"UPDATE users2 SET profile_pic='$rand_dir_name/$profile_pic_name' WHERE username='$user'");
}
else
{
echo "Invailid File! Your image must be no larger than 1MB and it must be either a .jpg, .jpeg, .png or .gif";
}
}
?>
I am able to upload my image to the database but the resize function doesn't seem to work and is unable to reduce the image size.
You are passing an url to imagejpeg function. The output file have to be the path to local directory as follows
$resized_file = "/home/rahulkapoor90/public_html/userdata/profile_pics/$rand_dir_name/resized_$profile_pic_name"
And apache must be allowed to write to the $resized_file
I have a piece of code that seems to work just fine on processing jpegs on a live server, but processing png's or gif's always give me black images. The funny thing is that on my test server, it processes png's just fine, but no gifs either. This function should work well universally on all mime types, but I guess php is having problems finding the file paths of anything with a file extension other than jpg. Anybody have any ideas on modifiying my function to process png's and gif's correctly, besides switching to python?
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio; //if original image width is greater than height
} else {
$h = $w / $scale_ratio; //if original image height is greater than width
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);//gd functions
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);//makes a black rectangle with width and height you specify
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if ($ext == "gif"){
imagegif($tci, $newcopy);
} else if($ext =="png"){
imagepng($tci, $newcopy);
} else {
imagejpeg($tci, $newcopy, 84);
}
}
$file_name = $_FILES["uploaded_file"]["name"]; // The file name
$path_suffix = pathinfo($file_name);
$path_ext = $path_suffix['extension'];
$target_file = "uploads/$file_name";
$list_file = "uploads/list_$file_name";
$wmax = 400;
$hmax = 400;
ak_img_resize($target_file, $list_file, $wmax, $hmax, $path_ext);
I have personally tried your code, didn't touch the function, but modified the bottom part only and it works just fine.
This is how my working script looks like (I am getting the file name from the URL via GET, for debugging):
<?php
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio; //if original image width is greater than height
} else {
$h = $w / $scale_ratio; //if original image height is greater than width
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);//gd functions
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);//makes a black rectangle with width and height you specify
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if ($ext == "gif"){
imagegif($tci, $newcopy);
} else if($ext =="png"){
imagepng($tci, $newcopy);
} else {
imagejpeg($tci, $newcopy, 84);
}
}
$file_name = $_GET['img']; // The file name
$path_ext = substr($file_name, -3);
$target_file = $file_name;
$list_file = 'list_' . $file_name;
$wmax = 400;
$hmax = 400;
ak_img_resize($target_file, $list_file, $wmax, $hmax, $path_ext);
?>
Note that I tested on several different png, jpg and gif files and they were all resized just fine and should work for u too, unless there is some problem with the original image files themselves.
Hope this helps.
I've got this custom function from DevelopPHP. For some reason it's not resizing the image to its specified size. If you see the code below, I am specifying the width ($wmax) and height ($hmax) of the image.
Below is the code that calls the function to resize the image:
$target_file = $location;
$fileExt = 'jpg';
$large_file = $_SERVER["DOCUMENT_ROOT"] . "/members/" . $id . "/large_" . $file_name . ".jpg";
$wmax = 600;
$hmax = 480;
ak_img_resize($target_file, $large_file, $wmax, $hmax, $fileExt);
This is the function that gets called by the above function:
<?php
// Adam Khoury PHP Image Function Library 1.0
// Function for resizing any jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
$img = imagecreatefromjpeg($target);
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
Your question is vague, but you should note that that function is not designed to re-size the image to exactly 600 x 480. It is designed to scale the image to preserve the aspect ratio. In other words, it is only going to exactly match either the height you specify, or the width, but not both. Otherwise the image would be distorted or cropped.