How to get image name automatically from destination folder - php

I am working on a project that resize images. My following code takes an image from upload directory, resizes it and save the output image but the problem is that I have to hard code image name.
I want to get image name automatically from upload directory. Please someone solve my problem.
<?php
include('resize_lib.php'); // resize_lib is the library that has functionality of how to resize the image
//focus on this line
$image_path = "upload/something.jpg";// hard coded image name
$resizeObj = new resize($image_path);
$resizeObj -> resizeImage(1536, 1024, 0); // width // height
$resizeObj -> saveImage("new.png", 100);
echo "done...";
?>

Try to get all images files from your uploaded directory .jpg or .png or .gif
$files = glob("upload/*.{jpg,png,gif}", GLOB_BRACE);
glob
Returns an array containing the matched files/directories, an empty
array if no file matched or FALSE on error.
You can then use foreach() loop to set your image name for $image_path. By the way you can also select only a single type of image e.g something.jpg
$files = glob('upload/*.jpg');

Related

how to get image file from absolute path in php

I have absolute path of my uploaded image file and i want to get image file which i uploaded in my folder. So how can i get image file and how i can upload image.So i can perform image operation like watermark and rotate. So i have to get image file in code igniter.
i tried but it is not working
$upld_file = 'my absolute path';
$real_path = realpath($upld_file);
$img = base64_encode($real_path);
and i send $img parameter to upload image
I am getting "you did not select a file to upload"
C:\wamp64\www\codeigniter_project\assets/images/1551892667300.jpeg
This is my absolute path of my image
Thank You.
You don't need to get the absolute path of the image if you are storing the images inside a assets in the root of your project directory, You can access the image like this
echo base_url('assets/images/1551892667300.jpeg');
NOTE : you need to load the url helper first
You can try like this..
//define image path
$filename="C:/wamp64/www/codeigniter_project/assets/images/1551892667300.jpeg";
// Load the image
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, 90, 0);
//and save it on your server...
imagejpeg($rotate, "savedestinationpath.jpg");
That should be the file input name. eg: If your file browse input name is myFileInput then it should be $this->upload->do_upload('myFileInput')
Also make sure that you have set attribute enctype="multipart/form-data" for your form.
One nice example is here

Cannot copy and resample an image with PHP

I am trying to copy a resampled image. Something seems not working:
copy($_FILES['image']['name'],'logo.png');
$dir_subida = 'C:\xampp5\htdocs\bild';
$source = imagecreatefrompng($_FILES['image']['name']);
$destination = imagecreatefrompng($dir_subida . '\\logo.png');
imagecopyresampled($destination,$source,0,0,0,0,110,55,600,220);
First I copy the image and save it in a directory. Then, I define the source (an image that I am uploading in a form). Then I define the destination.
Then I try to resample and copy it with the default function imagecopyresampled.
It just copies the image right away without any kind of changes.
Am I missing something?
You must save the destination image to a file after you manipulate it.
imagepng($destination, 'c:\\path\\to\\file.png');

Imagick: open file from path, resize, save to folder

I'm trying to append several images together into one big image, but the first hurdle is actually just getting the image to open.
I am attempting to do the following:
Open image from path
Size it with the specified dimensions
Save it to the current folder
I have the following:
define('WIDTH', 600);
define('HEIGHT', 800);
$img = new Imagick();
$img->readImage('dress.jpg');
$img->writeImage('image1.png');
I can get the image to save, but I do not know where to add the WIDTH and HEIGHT definitions?
I tried doing the following:
$img->newImage(WIDTH, HEIGHT, $img);
$img = new Imagick();
$img->readImage('dress.jpg');
$img->resizeImage(WIDTH, HEIGHT, Imagick::FILTER_LANCZOS, 1);
$img->writeImage('image1.png');
After "creating" an image resource with new Imagick() you are "working" in that resource. readImage() reads the image which is then present in the resource - after that you can resize the resource and write it out after the work is done.
See the documentation for more info Imagick::resizeImage()
With new Imagick(WIDTH, HEIGHT) you create an image resource with an empty canvas of the given size.

imagecopyresampled in php -> destination file

i'm using jCrop and CodeIgniter, trying to make an image uploader.
So i have my image folder and inside a temp folder.
I make the upload to my temp folder and then display the uploaded image with the cropper.
When i submit the form, i go to my php and use the code provided by jCrop:
$src = 'demo_files/flowers.jpg';
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
header('Content-type: image/jpeg');
imagejpeg($dst_r,null,$jpeg_quality);
Nor in here, or anywhere else, i see how to define the destination image path and filename.
I want to keep the original one in the temp folder and create the resized one in the parent folder.
Can someone help me please?
Nor in here, or anywhere else, i see how to define the destination image path and filename.
You'll find the manual page for imagejpeg() enlightening -- the second argument, which is null in your example code, is the destination file name.
I want to keep the original one in the temp folder and create the resized one in the parent folder.
Just be aware that the copy in the temporary directory may disappear once the script terminates.

File permission to PHP uploaded image

In my PHP site I have a script to upload images, see the script below
$uploaddir = $root_path."images/uploaded_images/category/";
$small_file_name = trim($_FILES['cat_image1']['name']);
$small_file_len = strlen($small_file_name);
$small_file_ext = strtolower(substr($small_file_name,-4)); // select last 4 characters
$small_uploadfile = $uploaddir. $_FILES['cat_image1']['name'];
if($small_file_len>4 and ($small_file_ext==".gif" or $small_file_ext==".jpg" or $small_file_ext=="jpeg")){
if ($small_file_ext=="jpeg")
$uniqname = uniqid(rand()).".".$small_file_ext;
else
$uniqname = uniqid(rand()).$small_file_ext;
$thumb_filename1 = "thumb_".$uniqname; // store uniqname into database
$uploadfile = $uploaddir.$uniqname; //uncomment for local testing
if (move_uploaded_file($_FILES['cat_image1']['tmp_name'], $uploadfile)) {
chmod($uploadfile,0777);
list($width, $height, $type, $attr) = getimagesize($uploadfile);
$max_width = 276;
$max_height = 162;
$a = new Thumbnail($uploadfile,$max_width,$max_height,$uploaddir.$thumb_filename1,100,'');
$a->create();
}else{
$flag=1;
$frm_server_side_error= $frm_server_side_error."Error in uploading image,";
}
}
else{
$flag=2;
$frm_server_side_error= $frm_server_side_error."Image not in gif or jpg format,";
}
After uploading in to the server its can't be see (I am displaying the thumb image). So I checked the file permission of that image through the FTP and give read permission. Thus image displays. But I give the read and write permission to the category folder and it’s parent folders. How can view the image after uploading (default read permission to the uploading files ie thumb image.)
I noticed that the actual image have read permission but the thumb image doesn't have the read permission. I need to display only the thumb image.
You have used a class Thumbnail to create a thumbnail image. But I guess you have missed to change the file permission in the new thumbnail image. Please check the code in the create() function of the class Thumbnail
Your code formatted poorly here (half is in code format, half not), but it appears you have a comment before the snippet that would copy the file from the temp location to the final location - if that's the case, the file won't exist, and thus won't be viewable.
But I could be wrong - I'm happy to take a second look if you fix the formatting.

Categories