I have 60 jpg images inside img folder
they are all of various dimensions
need all of them to be 1280 x 720
white background - if needed
using this code spinner on page works obut 10 seconds - meaning the code works something - but final result is - nothing
each image has the same dimension as before
pls help
function resize_no_crop($el, $w, $h) {
list($width, $height) = getimagesize($el);
$r = $width / $height;
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
$src = imagecreatefromjpeg($el);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
$arr = glob('img/*');
foreach($arr as $el){resize_no_crop($el, 1280, 720);}
echo 'finito';
You aren't saving the images:
$quality = 95;
foreach($arr as $el){
// Destinazione
$dest = dirname($el).DIRECTORY_SEPARATOR.basename($el, '.jpg') . '.cropped.jpg';
$resized = resize_no_crop($el, 1280, 720);
imagejpeg($resized, $dest, $quality);
}
Or you cal use $dest = $el to overwrite the original images (I never recommend this).
Use Imagick resizeImage:
foreach($arr as $el) {
$im = new \Imagick();
$im->readImage($el);
$im->resizeImage(1280, 720, \Imagick::FILTER_BOX, 1);
$im->writeImage($el);
$im->destroy();
}
From documentation: https://www.php.net/manual/en/imagick.resizeimage.php
Related
i'm beginner in php, i want to download some images And i want to cut it before saving from the bottom of the photo, Below is a code I write and works fine. There is only one problem. I think because the file is stored in the same name. Only one photo is saved, How to use the php hexdec or md5 function for each name-separated photo
$array[] = "http://up.abdolahzadeh.ir/view/2142160/5820073910.jpg";
$array[] = "http://up.abdolahzadeh.ir/view/2142169/4899388870.jpg";
foreach($array as $value) {
$im = imagecreatefromstring(file_get_contents($value));
$width = imagesx($im);
$height = imagesy($im);
$newwidth = $width;
$newheight = $height-20;
$offset_x = 0;
$offset_y = 0;
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopy($thumb, $im, 0, 0, $offset_x, $offset_x, $newwidth, $height);
imagejpeg($thumb,'myChosenName.jpg'); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
}
Try change the line
imagejpeg($thumb,'myChosenName.jpg');
For
imagejpeg($thumb, rand() '.jpg');
I am trying to resize and image with the following function:
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
After creating the function, I am trying to display the image after resizing by using this code but it is not working:
<?php
$img = resize_image('../images/avatar/demo.jpg', 120, 120);
var_dump($img); //The result is: resource(6, gd)
?>
<img src="<?php echo $img;?>"/>
PS: There is no problem with the inclusion of the function
You can't directly output an image that way. You can either:
Save the image to disk and enter the URL in the image tag.
Buffer the raw data, base64 encode it, and output it as a data URI (I wouldn't recommend this if you're working with large images.).
Approach 1:
<?php
$img = resize_image('../images/avatar/demo.jpg', 120, 120);
imagejpeg($img, '../images/avatar/demo-resized.jpg');
?>
<img src="<?= 'www.example.com/images/avatar/demo-resized.jpg' ?>"/>
Approach 2:
<?php
$img = resize_image('../images/avatar/demo.jpg', 120, 120);
ob_start();
imagejpeg($img);
$output = base64_encode(ob_get_contents());
ob_end_clean();
?>
<img src="data:image/jpeg;base64,<?= $output; ?>"/>
I have a little problem with GD library in PHP - I resize image and then I want crop it to 320px (width) / 240px (height). Let me say that resized image is 320px/300px. When I crop it, a 1-px black strip appears on the bottom of the image - I don't know why.
I'm using imagecrop, imagecreatefromjpeg and imagecopyresampled
Here's the example:
Thanks for your time.
The code
$filename = '../store/projects/project-123.jpg';
$mime = mime_content_type($filename);
list($w, $h) = getimagesize($filename);
$prop = $w / $h;
$new_w = 0;
$new_h = 0;
if ($prop <= 4/3) {
$new_w = 320;
$new_h = (int)floor($h*($new_w/$w));
} else {
$new_h = 240;
$new_w = (int)floor($w*($new_h/$h));
}
$thumb = imagecreatetruecolor($new_w, $new_h);
if (strcmp($mime,'image/png') == 0) {
header('Content-Type: image/png');
$source = imagecreatefrompng($filename);
} else {
header('Content-Type: image/jpeg');
$source = imagecreatefromjpeg($filename);
}
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
$filename = '../store/projects-thumbs/project-123.jpg';
$crop_data = array('x' => 0 , 'y' => 0, 'width' => 320, 'height'=> 240);
$thumb = imagecrop($thumb, $crop_data);
imagejpeg($thumb, $filename, 100);
imagedestroy($thumb);
imagedestroy($source);
imagecrop() has a known bug that causes the black bottom border to be added.
You can work around the problem using imagecopyresized(). See my answer to another SO question asking for an imagecrop() alternative.
I'm wanting to create a very very basic upload, resize, and crop PHP script.
The functionality to this will be identical (last i checked anyway) to the method Twitter uses to upload avatar pictures.
I want the script to take any size image, resize the shortest side to 116px, then crop off the top and bottom (or left and right side if it's landscape) as to get a square 116px by 116px.
I don't want a bloated PHP script with client side resizing or anything, just a simple PHP resize and crop. How is this done?
The GD Library is a good place to start.
http://www.php.net/manual/en/book.image.php
There a simple to use, open source library called PHP Image Magician. It uses GD but simplifies it's usage to 3 lines.
Example of basis usage:
$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');
If you want an example to work from my upload, resize and crop class does all of this plus some other cool stuff - you can use it all if needed or just take the bits out that you like:
http://www.mjdigital.co.uk/blog/php-upload-and-resize-image-class/
I don't think it is too bloated! - you can just do something this (not tested):
if((isset($_FILES['file']['error']))&&($_FILES['file']['error']==0)){ // if a file has been posted then upload it
include('INCLUDE_CLASS_FILE_HERE.php');
$myImage = new _image;
// upload image
$myImage->uploadTo = 'uploads/'; // SET UPLOAD FOLDER HERE
$myImage->returnType = 'array'; // RETURN ARRAY OF IMAGE DETAILS
$img = $myImage->upload($_FILES['file']);
if($img) {
$myImage->newWidth = 116;
$myImage->newHeight = 116;
$i = $myImage->resize(); // resizes to 116px keeping aspect ratio
// get new image height
$imgWidth = $i['width'];
// get new image width
$imgHeight = $i['height'];
if($i) {
// work out where to crop it
$cropX = ($imgWidth>116) ? (($imgWidth-116)/2) : 0;
$cropY = ($imgHeight>116) ? (($imgHeight-116)/2) : 0;
$cropped = $myImage->crop(116,116,$cropX,$cropY);
if($cropped) { echo 'It Worked (I think!)'; print_r($cropped);
} else { echo 'Crop failed'; }
} else { echo 'Resize failed'; }
} else { echo 'Upload failed'; }
I made this simple function which is very easy to use, it allows you to resize, crop and center an image to a specific width and height, it can suppert JPGs, PNGs and GIFs. Feel free to copy and paste it to your code:
function resize_imagejpg($file, $w, $h, $finaldst) {
list($width, $height) = getimagesize($file);
$src = imagecreatefromjpeg($file);
$ir = $width/$height;
$fir = $w/$h;
if($ir >= $fir){
$newheight = $h;
$newwidth = $w * ($width / $height);
}
else {
$newheight = $w / ($width/$height);
$newwidth = $w;
}
$xcor = 0 - ($newwidth - $w) / 2;
$ycor = 0 - ($newheight - $h) / 2;
$dst = imagecreatetruecolor($w, $h);
imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight,
$width, $height);
imagejpeg($dst, $finaldst);
imagedestroy($dst);
return $file;
}
function resize_imagegif($file, $w, $h, $finaldst) {
list($width, $height) = getimagesize($file);
$src = imagecreatefromgif($file);
$ir = $width/$height;
$fir = $w/$h;
if($ir >= $fir){
$newheight = $h;
$newwidth = $w * ($width / $height);
}
else {
$newheight = $w / ($width/$height);
$newwidth = $w;
}
$xcor = 0 - ($newwidth - $w) / 2;
$ycor = 0 - ($newheight - $h) / 2;
$dst = imagecreatetruecolor($w, $h);
$background = imagecolorallocatealpha($dst, 0, 0, 0, 127);
imagecolortransparent($dst, $background);
imagealphablending($dst, false);
imagesavealpha($dst, true);
imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight,
$width, $height);
imagegif($dst, $finaldst);
imagedestroy($dst);
return $file;
}
function resize_imagepng($file, $w, $h, $finaldst) {
list($width, $height) = getimagesize($file);
$src = imagecreatefrompng($file);
$ir = $width/$height;
$fir = $w/$h;
if($ir >= $fir){
$newheight = $h;
$newwidth = $w * ($width / $height);
}
else {
$newheight = $w / ($width/$height);
$newwidth = $w;
}
$xcor = 0 - ($newwidth - $w) / 2;
$ycor = 0 - ($newheight - $h) / 2;
$dst = imagecreatetruecolor($w, $h);
$background = imagecolorallocate($dst, 0, 0, 0);
imagecolortransparent($dst, $background);
imagealphablending($dst, false);
imagesavealpha($dst, true);
imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth,
$newheight,$width, $height);
imagepng($dst, $finaldst);
imagedestroy($dst);
return $file;
}
function ImageResize($file, $w, $h, $finaldst) {
$getsize = getimagesize($file);
$image_type = $getsize[2];
if( $image_type == IMAGETYPE_JPEG) {
resize_imagejpg($file, $w, $h, $finaldst);
} elseif( $image_type == IMAGETYPE_GIF ) {
resize_imagegif($file, $w, $h, $finaldst);
} elseif( $image_type == IMAGETYPE_PNG ) {
resize_imagepng($file, $w, $h, $finaldst);
}
}
All you have to do to use it is call it like so:
ImageResize(image, width, height, destination);
E.g.
ImageResize("uploads/face.png", 100, 150, "images/user332profilepic.png");
I have this piece of code that calls a random image and sends it to another page. That page calls this script via an image link. What I am trying to figure out is how to make it specify an image width when the image is past my max width. I got this far then got completely lost.
<?php
$folder = '';
$exts = 'jpg jpeg png gif';
$files = array(); $i = -1;
if ('' == $folder) $folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) {
if (preg_match('/\.'.$ext.'$/i', $file, $test)) {
$files[] = $file;
++$i;
}
}
}
closedir($handle);
mt_srand((double)microtime()*1000000);
$rand = mt_rand(0, $i);
$image =($folder.$files[$rand]);
if (file_exists($image))
{
list($width) = getimagesize($image);
$maxWidth = 150;
if ($width > $maxWidth)
{
header('Location: '.$folder.$files[$rand]); // Voila!;
}
else
{
header('Location: '.$folder.$files[$rand]); // Voila!
}
}
?>
You could use CSS max-width and max-height properties.
Here isa couple of resize functions, I think at least one of them maintains the aspect ratio.
function resize_image($file, $w, $h, $crop = false) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*($r-$w/$h)));
} else {
$height = ceil($height-($height*($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
And:
function thumb_aspect_crop($source_path, $desired_width, $desired_height) {
list( $source_width, $source_height, $source_type ) = getimagesize($source_path);
switch ($source_type) {
case IMAGETYPE_GIF:
$source_gdim = imagecreatefromgif($source_path);
break;
case IMAGETYPE_JPEG:
$source_gdim = imagecreatefromjpeg($source_path);
break;
case IMAGETYPE_PNG:
$source_gdim = imagecreatefrompng($source_path);
break;
}
$source_aspect_ratio = $source_width / $source_height;
$desired_aspect_ratio = $desired_width / $desired_height;
if ($source_aspect_ratio > $desired_aspect_ratio) {
// Triggered when source image is wider
$temp_height = $desired_height;
$temp_width = (int) ( $desired_height * $source_aspect_ratio );
} else {
// Triggered otherwise (i.e. source image is similar or taller)
$temp_width = $desired_width;
$temp_height = (int) ( $desired_width / $source_aspect_ratio );
}
// Resize the image into a temporary GD image
$temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
imagecopyresampled(
$temp_gdim,
$source_gdim,
0, 0,
0, 0,
$temp_width, $temp_height,
$source_width, $source_height
);
// Copy cropped region from temporary image into the desired GD image
$x0 = ( $temp_width - $desired_width ) / 2;
$y0 = ( $temp_height - $desired_height ) / 2;
$desired_gdim = imagecreatetruecolor($desired_width, $desired_height);
imagecopy(
$desired_gdim,
$temp_gdim,
0, 0,
$x0, $y0,
$desired_width, $desired_height
);
return $desired_gdim;
}
Try to use Primage class, like in example
You can not specify that in the header. There are 2 ways to go about it.
1 : in the image tag, specify the width attribute, however this will force the smaller images to scale up as well, so probably not the best course of action.
2 : Resize the image on the fly with GD library and send the resultant image instead of the original.
Edit:
For resizing you may look into this very simple to use image resize class.
http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resizeToWidth($maxWidth);
$image->save('picture2.jpg');
Now you can redirect to picture2.jpg or send the image content inline. Also if the image is used often, then check on top of the script, if your resized image exists, if it does send it otherwise continue to resize.