Magento : add custom php code into product description - php

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.

Related

How to save an image to directory after resize in php?

I am using the GD libary in php to resize uploaded images which works fine but I just can't figure out how to save the resized image to a directory. How do I go about this? I don't seem to get any errors.
<?php
// The file
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$filename = 'test.jpg';
$percent = 0.5;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$filename = $_FILES['photoimg']['tmp_name'];
// 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
imagejpeg($image_p, null, 100);
//This is my attempt to save the image that does not work
if (move_uploaded_file($image, "memberFiles/saved_image.jpg")) {
}
}
?>
Just use:
imagejpeg($image_p, 'some/other/existing/directory/result.jpg');

PHP image resize isn't working

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)."'>";
?>

Resizing image using php

I am trying to resize my image by making it 200 x 200 while keeping the aspect ratio. I am trying to follow http://php.net/manual/en/function.imagecopyresampled.php to try to accomplish this. Here is my code:
<?php
$directory = "uploads/";
$images = glob($directory."*.jpeg");
foreach($images as $filename) {
// Set a maximum height and width
$width = 200;
$height = 200;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
}
?>
I took out the content type because it was giving me an error. Instead of outputting the resized image, it was outputting bunch of symbols like this:
JFIF�����t��jw��E��4."�DN�9>m���4����P};WI��x"�.
Anybody know why this is happening?
You need to sent header to a browser so your output could be properly understand by browser. Add following line to your script:
header('Content-Type: image/jpeg');
You need to have the header('Content-Type: image/jpeg'); before the foreach loop
This code must be at the very beginning of your file:
header('Content-Type: image/jpeg');
And the above code should be standalone, you shouldn't have any HTML tags anywhere and it shouldn't be "PHP-included" anywhere.

Using imagejpeg() save to folder but doesn't preview image on the browser

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";
?>

Downloading an image with php and at the same time resizing it

I want to download this image:
http://imgs.xkcd.com/clickdrag/1n2w.png
But the image is too large for me so i want to resize it to lets say 100 times smaller than it is now. Also i want the image to have its original name (in this case 1n2w.png).
For downloading i was thinking of using somet
$content = file_get_contents('http://imgs.xkcd.com/clickdrag/1n2w.png');
file_put_contents('/images', $content);
But it didnt work. Maybe i need to use curl for this?
As for the resizing part i dont know what to use, so if possible i would like too see some suggestions on this.
For image re-sizing you can use it.
$percent = 0.5;
$filename = '/home/Pictures/downloaded_file.jpg';
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename)
;
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, null, 100);

Categories