I have a folder named images, where I have images like: hero.jpg, hero_medium.jpg, hero_small.jpg. My question is, if this is the correct solution, or maybe it would be better to have one big picture and change its size via URL. At the moment I have managed to do something like this in img.php file in images folder:
<?php
header('Content-Type: image/jpeg');
$filename = 'hero.jpg';
list($width_orig, $height_orig) = getimagesize($filename);
if(empty($_GET['w'])){
$width = $width_orig;
} else {
$width = $_GET['w'];
}
$height=$width;
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, null, 100);
I would like to change the size of a photo by a friendly URL, i.e. hero_medium.jpg to e.g. 768px in php using htaccess, is such a thing possible?
EDIT: Apple has an interesting situation on its website. The URL for the images looks like the following: https://www.apple.com/newsroom/images/environments/stores/Apple_Tower-Theatre-now-open-in-downtown-LA-store-interior-wide-shot_062421_big.jpg.medium.jpg, why is there big.jpg.medium.jpg? I suspect they may be doing something with htaccess file, because having that many photos it would be unreadable, so I think they are resizing them dynamically. What do you think?
EDIT 2: I noticed that it takes much longer to load and change the image via php, is this actually a good idea?
If you just want to return an image of adequate dimensions, there are several ways to achieve this.
Maybe you want to have a look at this:
https://css-tricks.com/a-guide-to-the-responsive-images-syntax-in-html/
Related
I have stored multiple images in base64 inside my database. I get images using php as image path. But I want to reduce size of my image when decoding it from base64, because it slows down my app if I load full size image. (Full size image I need just in backend).
/*
DB stuff getting base64 string from database
$img = base64 string (can be with 'data:image/jpg;base64,' in front, thats for the str_replace())
*/
if($img){
header("Content-Type: image/png");
echo base64_decode(str_replace("data:image/jpg;base64,","",$img));
}
Everything works nice this way. I use it like this:
<img src="http://example.com/getimg.php?id=4" />
or in css. I need this because of security reasons, I cant store any image on server, also in path I have access_token variable, so random person cant see images.
Is there a way to do this without storing the actual image in server?
You can use imagecreatefromstring and imagecopyresized.
Live example here
<?php
if ($img) {
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
$data = base64_decode($img);
$im = imagecreatefromstring($data);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$thumb = imagecreatetruecolor($newwidth, $newheight);
// Resize
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
}
I need resize Image from FTP like /images/1_2.jpg. I need to load it to imagecreatefromjpeg();. Now my script looks like this.
$file = "images/1_2.jpg";
$imagesize = getimagesize($file);
$img = imagecreatefromjpeg($file);
$width = 70;
$height = 100;
$img2 = imagecreatetruecolor($width, $height);
imagecopyresampled($img2, $img, 0, 0, 0, 0, $width, $height, $imagesize[0], $imagesize[1]);
imagejpeg($img2, "images_mini/1_2.jpg");
My problem is that images don't upload to FTP, its shows me everything is ok but i can't find images on FTP.
Sorry I have it, just only bad converted variable, i using cross languages now for application so it is dificult to check everythink. Thank you anyway.
using the example given at http://www.php.net/manual/en/function.imagecopyresized.php ... how to get image sizes afterward using getimagesize() function?
CODE:
<?php
if(isset($_FILES['images'])){
//TEST1:
$img = resize_this_image_now($_FILES['images']['tmp_name']);
//TEST2:
$img = resize_this_image_now($_FILES['images']['name']);/// This Drastically failed.
$new_image = getimagesize($img);
var_dump($new_image[0]);// I guessed this should have printed out the WIDTH_OF_THE_IMAGE... but, it prints some NON_READABLE stuffs (why?)
}
// The PHP.NET CODE in a Function
function resize_this_image_now($filename){
// File and new size
// $filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
return imagejpeg($thumb);
}
?>
All I want is to get the Size of the Image.... also, is it possible to do something like:
$_FILES['images']['tmp_name'] = $the_newly_resized_image_returned_from_the_PHP_dot_NET_code'; .... So that the ['images']['tmp_name'] will now have as source this new image??
Any suggestion is highly appreciated...
I decided to spend sometime examining you question. What I found out is that, I don't think you'd need to return the resized image through imagejpeg() the way you did. You might also need to add a imagedestroy(), after you call imagejpeg() in you function, to destroy the temporary memory used.
You need to first fully Upload the Image before resizing it. If you'd like, you could send the image in a temporary storage while you do whatever you want to it so that Php does not have to deal with it in a 'tmp_name' format...Then You can destroy the image later on.
Once the image is fully uploaded, things become easier. The codes might look something like:
if(isset($_FILES['images'])){
//may be some random numbers to accompany it.
$rand = floor((mt_rand()+rand()+mt_rand())/3);
//Send it to the temporary folder you have had to create.
if(move_uploaded_file(
$_FILES['images']['tmp_name'],'temporary_storage/image_'.$rand.'.jpg')){
//Then run the `resize` function from here.
$image_resized = resize_this_image_now('temporary_storage/image_'.$rand.'.jpg');
//Now You can get the size if you wish.
list($width,$height) = getimagesize('temporary_storage/image_'.$rand.'.jpg');
// Out put
echo "W:".$width."<br>H:".$height;
//After you use it as desired, you can now destroy it using unlink or so.
unlink('temporary_storage/image_'.$rand.'.jpg');
}else{
echo "Upload Error goes here";
}
}
Note this answer is produced after several trials and errors... Please use this strategy wisely.
Hope it helps.
Is there any script that resizes an external image and outputs it ?
For example, i want to resize all external square images to 130X130.
Like this
http://mydomain.com/script/resize.php?url=http://otherdomain.com/image.png
EDIT:
Facebook also is an example maybe
https://s-external.ak.fbcdn.net/safe_image.php?d=AQCYX3NIE5gMyujT&url=http%3A%2F%2Fi2.ytimg.com%2Fvi%2FyoLeJNjIVZk%2Fhqdefault.jpg
any help appreciated.
Thanks
// Content type
header('Content-Type: image/jpeg');
//get image from internet and save it into local disk
$url = 'http://www.google.com/images/srpr/logo3w.png';
$img = 'google.png';
file_put_contents($img, file_get_contents($url));
//get current size and set new size
list($width, $height) = getimagesize($img);
$new_width = 130;
$new_height = 130;
// genarate resized image copy
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefrompng($img);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// flush image to browser
imagejpeg($image_p, null, 100);
// save resised image to disk
imagejpeg($image_p, "newimage.jpg",100);
Use this code only if you want to resize and save on your application otherwise above example is enough.
Take a look at this thread. You can write your own code to resize images or use TimThumb script.
if you dont want to save them than you can just set the with and hight with html or css
<img height="130px" with="130px;" src="somesite.com/img.jpg"></img>
So I already know how to combine two images and keep transparency using the GD image library with php however I need to be able to pull images from the server while keeping the dimensions in the power of two so that I can use them as textures to be applied to 3d objects in opengl.
So my real question is how can I place a scaled version of an image that keeps its original proportions within a transparent one that has a 256x256 or 128x128 dimension. Also Id like to place the resized image within the center of the completely transparent one.
Some help on this would be awesome.
Check out this references, I think it's what you want: http://php.net/manual/en/function.imagecopyresized.php
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>