I found the following script from PHP site to half the size of the image. I use the database to fetch the link of the image. Other things are working correctly which means there isn't any kind of error anywhere except this one.
echo "<img src='".// File and new size
$filename = '$row["image"]';
$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);
"'>"
Error:
The whole page is destroyed and there is a broken link of an image.
Hope you guys will help me!
It works after you drop those lines:
echo "<img src='".// File and new size
"'>"
header does it for you, informs that there is jpg coming, no need to echo image tag.
The other solution is to remove this line:
header('Content-Type: image/jpeg');
And create new file, then use it as a source of an image:
// Output
$new_filename = 'new_image.jpg';
imagejpeg($thumb,$new_filename);//saves new image to a file, instead of outputting it to the screen
echo "<img src='$new_filename'>";
Try this it works
<?php
$filename = '$row["image"]';
$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
echo "<img src='".imagejpeg($thumb)."'>";
?>
Related
I am trying to load an image from an external URL, then resize it and show it in a PDF. I am trying to achieve it using a single image for now, but the whole functionality will be inside a foreach loop for handling a lot of very large images.
Firstly, I resized the image, then get the contents of the image, applied base65 encoding, built up a source string from that and added the string to my img src tag. Here's my code -
$filename = 'https://jooinn.com/images/nature-319.jpg'; // URL of the image
$percent = 0.25; // percentage of resize
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
$imageData = base64_encode(file_get_contents($image_p));
// Format the image SRC: data:{mime};base64,{data};
$src = 'data: '.mime_content_type($image_p).';base64,'.$imageData;
// Echo out a sample image
echo '<img src="' . $src . '">';
imagedestroy($image_p);
I think the problem is with this line $imageData = base64_encode(file_get_contents($image_p));, I'm doing it wrong. It works nicely with URL's but how can I make it work for a resized image here? For example, the following works perfectly as long as I don't use the resized image -
$filename = 'https://jooinn.com/images/nature-319.jpg'; // URL of the image
// Output
$imageData = base64_encode(file_get_contents($filename));
// Format the image SRC: data:{mime};base64,{data};
$src = 'data: '.mime_content_type($filename).';base64,'.$imageData;
// Echo out a sample image
echo '<img src="' . $src . '">';
Indeed, as you said, the following line in the code is wrong:
$imageData = base64_encode(file_get_contents($image_p));
The $image_p variable is not a filename but a resource created by imagecreatetruecolor.
You first have to convert it to a jpeg file using imagejpeg().
You can avoid saving an intermediate file before encoding to base64 using the ob_*xxx* functions
ob_start();
imagejpeg($image_p);
$imageContent = ob_get_contents();
$imageData = base64_encode($imageContent);
ob_end_clean();
You also have a problem with this line, again as $image_p is not a filename:
$src = 'data: '.mime_content_type($image_p).';base64,'.$imageData;
As you are creating a jpeg file you should just replace it with:
$src = 'data: image/jpeg;base64,'.$imageData;
For convenience here is the full working script:
$filename = 'https://jooinn.com/images/nature-319.jpg'; // URL of the image
$percent = 0.25; // percentage of resize
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
ob_start();
imagejpeg($image_p);
$imageContent = ob_get_contents();
$imageData = base64_encode($imageContent);
ob_end_clean();
// Format the image SRC: data:{mime};base64,{data};
$src = 'data: image/jpeg;base64,'.$imageData;
// Echo out a sample image
echo '<img src="' . $src . '">';
imagedestroy($image_p);
Is there something I am missing here? If I change imagejpeg($thumb, $newImage);
to imagejpeg($thumb); it echos a load of unreadable characters. The thumb image directory exists.
My ultimate aim is to copy an image at a lower quality.
$filename = $imageDirectory . '/1.jpg';
$percent = 0.5;
$newImage = $imageDirectory . '/thumbs/1.jpeg';
echo "image: <br>";
echo $filename;
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, $newImage);
UPDATE: I now realize that the second parameter must be an image location with the new name. So... I have redefined $newImage. The path is fine... if I upload an image named 1.jpg to that location manually it exists at that path.
You'll have to add the correct Content-Type header for the response if you want to output the image to the browser:
header('Content-Type: image/jpeg');
imagejpeg($yourimage);
Please check the first example in the documentation for more information. If you want to decrease the quality before outputting to your browser check the third example.
I used the code below to get an image:
$url = "http://maps.googleapis.com/maps/api/staticmap?size=800x600&zoom=12¢er=(10.763705518561297,106.64444580078126)";
$content = file_get_contents($url);
I received a 640px x 640px image meanwhile I expected a 800px x 600px one. How do I scale that image to 800px x 600px?
Try doing this:
<?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);
?>
Here, rescaling is done by 50%, you can change parameter as per your requirement. You can even use Imagick for image manipulation. See, if that helps.
Referred: PHP Manual
I wrote a php script that resizes jpeg images provided by external URL.
The script works well on my server with a test page, but it seems that Magento doesn't allow custom php code into product description.
Is there any way to insert that piece of code into description ?
<?php
header('Content-Type: text/html; charset=utf-8');
$url_A = 'http:/...';
$url_B = 'http:/...';
$resizer_URL = 'http:/...';
echo '<img class="scanA" src="'.$resizer_URL.'?url='.$url_A.'"/>
<img class="scanB" src="'.$resizer_URL.'?url='.$url_B.'"/>';
?>
The script code is:
<?php
// The file
$filename = $_GET['url'];
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$ratio = $height / $width;
$new_width = 580;
$new_height = $new_width * $ratio;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 75);
//
imagedestroy($image_p);
imagedestroy($image);
?>
Thanks.
Index.php
<?php
error_reporting(E_ALL);
// File and new size
//the original image has 800x600
$filename = 'images/lazy1.jpg';
//the resize will be a percent of the original size
$percent = 0.5;
// Content type
header('Content-Type: image/jpg');
// 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 and free memory
//the resized image will be 400x300
imagejpeg($thumb, "raj.jpg", 100);
imagedestroy($thumb);
echo "Image Resize Successfully";
?>
I am using imagejpeg() to save file. i want save file in folder but not show on browser. in browser only show this message "Image Resize successfully".
<?php
error_reporting(E_ALL);
// File and new size
//the original image has 800x600
$filename = 'images/lazy1.jpg';
//the resize will be a percent of the original size
$percent = 0.5;
//Removed lines here
// 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 and free memory
//the resized image will be 400x300
imagejpeg($thumb, "raj.jpg", 100);
imagedestroy($thumb);
echo "Image Resize Successfully";
?>