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');
Related
I have been trying to merge two transparent images using below code.Base image is a fully transparent image used as a canvas.
I'm getting a result like the image below(1st image - result.png). How to make it fully transparent?
function copyTransparent($src, $output, $srcWidth){
$x = 288;
$y = 85;
$bg = 'tranparent.png'; // fully transparent image as bg.
$margRight = abs(($x- $srcWidth)/2);
$img = imagecreatefrompng($bg);
imagealphablending($img, false);
imagesavealpha($img, true);
// Copy from other
imagecopy($img,$src, $margRight, 0, 0, 0, $x, $y);
// Save PNG
imagepng($img,$output,9);
imagedestroy($img);
}
$png = 'testSign2.png';
list($width,$height) = getimagesize($png);
$src = imagecreatefrompng($png);
$newHeight = 85;
$ratio = $width / $height;
$newWidth = ceil($newHeight*$ratio);
$newImage = resizePng($src, $newWidth, $newHeight);
copyTransparent($newImage,"result.png", $newWidth);
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
As the title says ..How to move/rename image to new folder?
I have this so far and the new image is resized/cropped but it doesn't move to "new/" folder:
$in_filename = '4csrWqu9ngv.jpg';
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 65;
$new_width = $width;
$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
header('Content-Type: image/jpeg');
imagejpeg($new_image);
$move_new = imagejpeg($new_image);
rename($move_new, 'new/' . $move_new);
As always any help is appreciated :)
You had few mistakes in your code. Output of imagejpeg is a boolean, so your rename always failed. You also never saved resized image. You have to use 2nd parameter of imagejpeg and provide proper filename of new image. Also, make sure directory new exists, else rename will fail.
Fixed code:
$in_filename = '4csrWqu9ngv.jpg';
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 65;
$new_width = $width;
$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
/* Uncomment in case you want it also outputted
header('Content-Type: image/jpeg');
imagejpeg($new_image);
*/
imagejpeg($new_image, $in_filename);
rename($in_filename, 'new/' . $in_filename);
Does the "new" folder exist? If not, you need to create it at first using mkdir.
I want to take out the text in the bottom of an image. How can I cut it from bottom ...say 10 pixels to cut from bottom.
I want do this in PHP. I have lots of images that have text in the bottom.
Is there a way to do it?
Here you go.
To change the name of the image, change $in_filename (currently 'source.jpg'). You can use URLs in there as well, although obviously that will perform worse.
Change the $new_height variable to set how much of the bottom you want cropped.
Play around with $offset_x, $offset_y, $new_width and $new_height, and you'll figure it out.
Please let me know that it works. :)
Hope it helps!
<?php
$in_filename = 'source.jpg';
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 15;
$new_width = $width;
$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
header('Content-Type: image/jpeg');
imagejpeg($new_image);
?>
You may use the GD Image Library to manipulate images in PHP. The function you're looking for is imagecopy(), which copies part of an image onto another. Here's an example from PHP.net that does roughly what you describe:
<?php
$width = 50;
$height = 50;
$source_x = 0;
$source_y = 0;
// Create images
$source = imagecreatefromjpeg('source.jpg');
$new = imagecreatetruecolor($width, $height);
// Copy
imagecopy($source, $new, 0, 0, $source_x, $source_y, $width, $height);
// Output image
header('Content-Type: image/jpeg');
imagejpeg($new);
?>
To crop the source image, change the $source_x and $source_y variables to your liking.
Hey, i am looking for a method to creat temporay thumb file in PHP. Is there any way not to store the image on the server or delete them right after. What I am looking for is a solution like this: http://www.dig2go.com/index.php?shopbilde=772&type=1&size=120
Could some one explain to me how the php code behind this works? For the moment are am using a php code I found on the net for creation of thumb nails:
function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight, $saveNameAndPath)
{
if(!file_exists($pathToImage))
return false;
else
{
//Load image and size
$img = imagecreatefromjpeg($pathToImage);
$width = imagesx($img);
$height = imagesy($img);
//Calculate the size of thumb
$new_width = $thumbWidth;
$new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));
//Make the new thumb
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//Copy the old image and calculate the size
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//Save the thumb to jpg
imagejpeg($tmp_img, $saveNameAndPath);
return true;
}
}
function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight, $saveNameAndPath)
{
if(!file_exists($pathToImage))
return false;
else
{
//Load image and size
$img = imagecreatefromjpeg($pathToImage);
$width = imagesx($img);
$height = imagesy($img);
//Calculate the size of thumb
$new_width = $thumbWidth;
$new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));
//Make the new thumb
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//Copy the old image and calculate the size
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// sets the header and creates the temporary thumbnail
// ideal for ajax requests / <img> elements
header("Content-type: image/jpeg");
imagejpeg($img);
return true;
}
}
You can use ini_set("memory_limit","12M"); to set memory limit in your script. You can extend it from 12 megabytes to maximum memory you have.
Generally 64M is good.