resize image before copy? - php

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

Related

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

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

How to auto resize image when upload using php

i want to make my image size as my given size when it upload.
my php code
$picture_name =time().'-' .$_FILES["picture"]["name"];
if($c->Insert())
{
move_uploaded_file($_FILES["picture"]["tmp_name"], "Portfolio_Image/" . $picture_name);
}
But how can i re-size image at upload time
Take a look at the PHP GD library. In particular the imagecopyresized function. The example on there is a pretty good one.

Convert an image from progressive to interlaced without losing quality or details?

I'm trying to retrieve large images from a directory outside of the root directory. At the moment I just use "fpassthru", but this loads the image either progressively or interlaced depending on what is was when uploaded.
How do I create a complete copy of an image but convert it to interlaced without losing any quality or detail with PHP?
If you use the GD libraries that come with PHP, you can use imageinterlace to accomplish this.
Here's from the example:
<?php
// Create an image instance
$im = imagecreatefromgif('php.gif');
// Enable interlancing
imageinterlace($im, true);
// Save the interlaced image
imagegif($im, './php_interlaced.gif');
imagedestroy($im);
?>
Alternately, you could use ImageMagick.

How can I resize an image already uploaded using Magickwand(PHP)/ImageMagick?

I am working on building gallery where the user uploads all the images. I had tried to use GD originally but found that it used way too much memory when dealing with images from a digital camera. So I have been looking into ImageMagick and ran into this problem.
My end goal is to resize the image and then upload it. I am not sure if this is possible with ImageMagick or not. I have gotten it to resize the image after upload but it doesn't save the resized image, just the original size.
This is the code I am currently using: ($image is the path to the file on my server)
$resource = NewMagickWand();
MagickReadImage($resource,$image);
MagickSetImageCompressionQuality( $resource, 100);
$resource = MagickTransformImage($resource,'0x0','660x500');
Any input would be appreciated,
Levi
Your code will send the modified image to the client (the web browser), but it will not save it to the server (replacing the original image, for example)
To save the image, use:
MagickWriteImage( $resource, 'new_image.jpg' );

Categories