Cannot copy and resample an image with PHP - 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');

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

How to get image name automatically from destination folder

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');

Overwrite image in php while uploading

I want to know that how can I overwrite images when they uploaded to server in php. For example I uploaded a photo to a folder as soon as I upload another image it will take place of previous image. Image name is not same it may differ. Thanks
Delete the previous image before uploading the new one using:
$path = $_SERVER['DOCUMENT_ROOT'].'img/img.jpg';
unlink($path);
This code basically assigns the path of the image to $path and deletes the image using unlink($path);

resize image before copy?

I'm using the PHP copy function to copy an image after upload to a directory. Is there a way to resize the image before I copy it? I need to resize it to 80x80 for use in my forum.
Here is how I'm calling my copy:
//lets move a copy to the img dir for the forum
$avatar_tmp_file = 'forum/img/avatars/'.$_SESSION['forum_uid']. '.jpg';
copy($_FILES['file']['tmp_name'], $avatar_tmp_file);
Any suggestions?
Yes there is a way to do so. using imagick for instance.
$image=new Imagick();
$image->readImage($_FILES['file']['tmp_name']);
$image->thumbnailImage(80,80);
$image->writeImage($avatar_tmp_file);

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.

Categories