symfony pass resource identifier to template from action - php

my action:
public function executePreview(sfWebRequest $request)
{
$this->setLayout('layout_preview');
$text='';
$text= $request->getGetParameter('text');
$this->img='';
$this->img2=$this->createImage(array('font_size'=>10, 'line_spacing'=>1.5,'font'=>'dejavu', 'angle'=>10,
'preset'=>'BRCA', 'text'=>$text));
}
public function createImage( $params)
{
$x=0;
$y=0;
$interval= $params['font_size']*$params['line_spacing'];
$src_image= imagecreatefromjpeg(sfConfig::get('sf_web_dir').'/images/'.$params['preset'].'.jpg');
$black = imagecolorallocate($src_image, 0, 0, 0);
$lines=explode("\n", $params['text']);
putenv('GDFONTPATH='.join(':',sfConfig::get('app_font_path')));
$fontname=$params['font'].'.ttf';
foreach($lines as $i=>$line):
imagettftext($src_image, $params['font_size'], $params['angle'], $x, $y+$i*$interval, $black, $fontname, $line);
endforeach;
return $src_image;
}
in my template:
<?php
imagejpeg($img2);
?>
but when trying to GET /modulename/preview?preview=something&text=somethingelse, it gives an error. viewing the source of the webpage obtained, I see :
<title>InvalidArgumentException: Unable to escape value "NULL".</title>
Can I not pass resource identifier to the template? What can be a way around that? I need this createImage function elsewhere also, I'm just trying to follow DRY

imagejpeg creates the image and returns a boolean, i don't get why you call it in your view, it should stay to your action, actually i wouldn't use view at all in your case.
try something like :
$this->getResponse()->setHttpHeader('Content-type', 'image/jpeg');
$this->getResponse()->setContent($src_image);
$this->setLayout(false);
or directly setting header with PHP and returning sfView::HEADER_ONLY

Related

pass Imagick output to another function with Imagick (no saving)

I have two function in two separate class, both have some Imagick processing.
I want the first Imagick to resize image and then send it to another Imagick instance without saving it as a file.
In the first class crop
function cropimage($inputfile){
$image = new Imagick($inputfile);
$image->cropimage($w, $h, $x, $y);
# and here I wish to return some $image->output???() which let me to use it in second class
return ????;
}
And in second class resize
function resizeImage(){
$crop = new crop(); // my first class instance
$image_to_work = $crop->cropimage($this->image); // output from first class
$image = new Imagick($image_to_work);
$image->resizeImage($width, $height);
$image->writeImage($outputfile);
}
Any ideas?
Thanks in advance.
Cheers
Images can be passed around as variables:
function cropimage($inputfile) {
$image = new Imagick($inputfile);
$image->cropimage($w, $h, $x, $y);
return $image;
}
function resizeImageAndSave($image) {
$image->resizeImage($width, $height);
$image->writeImage($outputfile);
}
$image = cropimage("./test.png"); // return the image
resizeImageAndSave($image); // pass it into the next function
Use sessions.
First, make sure you have session_start() in every script that will utilize sessions.
Then do something like $_SESSION['image'] = $image and then your next script can access that session variable.

laravel 5.1 queues undefined function in handle() but working in constructor

I use Laravel 5.1 queues to process large images. I've a very strange behavior since when I call a function "imagettfbbox" in the constructor it works. But obviously I need to make it work in the "handle" but there I got an error.
public function __construct()
{
//TEST
$font_path = public_path('/fonts/roboto/Roboto-Thin.ttf');
imagecreate(10,10); //works!
imagettfbbox(10, 0, $font_path, 'test'); //works!
}
public function handle() //GenerateImage $generator, Image $img
{
//TEST
print 'OK'; //gets printed
$font_path = public_path('/fonts/roboto/Roboto-Thin.ttf');
imagecreate(10,10); //works!
imagettfbbox(10, 0, $font_path, 'test'); //CRASHES!
}
I get the 'OK' printed and then the error "Call to undefined function App\Jobs\imagettfbbox()".
It is a very strange behavior since some image functions work other not. I've GD installed and everywhere outside handle the code works. Any clue what I'm missing here?
Maybe your are missing FreeType library that is needed by imagettfbbox

How to use PHP drawing functions with Laravel

I'm trying to use PHP shape drawing functions with the Laravel framework but I am having some trouble.
So, I just to test it out, basically just copied the example from php.net and used it in a closure and a test route.
Route::get('maptest', function()
{
$image = imagecreatetruecolor(200, 200);
$red = imagecolorallocate($image, 227, 32, 32);
imagefilledrectangle($image, 100, 80, 50, 25, $red);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
});
doing it this way works fine.
the problem is when I throw a Controller and a View into the mix. When I set the headers and what not and create a Response, I get a little broken picture icon in the top left corner of the window and nothing on the page loads at all.
this is how I am setting the headers.
(I know this is posted everywhere, but none of the solutions I could find seemed to work for me.)
public function show($id)
{
$user = User::find($id);
$view = View::make('profile/home', ['user' => $user]);
$response = Response::make($view, 200);
$response->header('Content-type', 'image/png');
return $response;
}
So the result of the above code (inside my controller) gives me a little broken picture icon in top left corner of window.

captcha class wont display image if an instance is made outside of class file

I made a captcha class to generate a captcha. for some reason I can only get the image to display if I make an instance of the class in the class file itself. otherwise I get nothing. In firebug I see the file being included but its being sent as html despite I used header("Content-type: image/png");
class.captcha.php
<?php
class captcha
{
public $length=4;
public $width=150;
public $height=50;
public $allowedChar='1234569ACEFGHJKMNPQRSTXZ';
public $font='Arcade.ttf';
public $fontSize=25;
public $fontRed=0;
public $fontGreen=0;
public $fontBlue=104;
public $backgroundRed=204;
public $backgroundGreen=204;
public $backgroundBlue=255;
public $noiseRed=0;
public $noiseGreen=0;
public $noiseBlue=104;
public $noisePercent=5;
/*
** #RETURN IMAGE raw image of captcha
*/
public function create($name)
{
//string
for ($i=0;$i<$this->length;$i++)
{$captcha.= $this->allowedChar[mt_rand(0, strlen($this->allowedChar))];}
//image
$img=imagecreatetruecolor($this->width, $this->height);
//colors
$bgColor=imagecolorallocate($img, $this->backgroundRed, $this->backgroundGreen, $this->backgroundBlue);
$fontColor=imagecolorallocate($img, $this->fontRed, $this->fontGreen, $this->fontBlue);
$noiseColor=imagecolorallocate($img, $this->noiseRed, $this->noiseGreen, $this->noiseBlue);
imagefilledrectangle($img,0,0,$this->width+1,$this->height+1,$bgColor);
//text
for($i=0;$i<strlen($captcha);$i++)
{
$rotate=rand(0, 80)-40;
$offX=$i*$this->fontSize+rand($this->fontSize, $this->fontSize*1.2);
$offY=$this->fontSize+rand($this->fontSize/2, $this->fontSize);;
$letter=$captcha[$i];
imagettftext($img, $this->fontSize, $rotate, $offX, $offY, $fontColor, $this->font, $letter);
}
//add noise
for($i=0;$i<$this->height*$this->width*($this->noisePercent/100);$i++)
{
//noise
$x=rand(0, $this->width);
$y=rand(0, $this->height);
imagesetpixel($img, $x, $y, $noiseColor);
}
$_SESSION[$name]=md5($captcha);
return $img;
}
}
?>
captcha.contact.php
<?php
session_start();
include('class.captcha.php');
$c=new captcha;
header("Content-type: image/png");
imagepng($c->create('contactCaptcha'));
?>
link to above if it helps http://yamikowebs.com/_test/_php/class.captcha.php
the error is The image “http://yamikowebs.com/_test/_php/captcha.contact.php” cannot be displayed because it contains errors.
if i put the contents of capture.contact.php at the end of class.captcha.php it works no problem.
I think the solution may be the fact you are using the closing tags of PHP:
?>
They are not required, but sometimes harmful. If there is any sign after such tag, the headers that you are trying to send will not be sent and the notice may appear (depending on the error_reporting setting).
Just remove closing tags (?>) and this should be ok.
One additional thing - if something is required for the script to work (such as your class file), use require() instead of include().
contact.captcha.php
<?php
include('class.captcha.php');
$c=new captcha;
header("Content-type: image/png");
imagepng($c->create('contactCaptcha'));
?>
session_start() sends header information so once removed it started working fine.

PHP checking function output

I have a basic function to resize image given below:
function resize() {
.....
...
..
imagejpeg($image_new,$img_path,80);
imagedestroy($image_old);
imagedestroy($image_new);
}
I need to call it in another function and see its output. How can i do that? I want something like:
function test(){
//calling resize function
if (resize() === true){
echo "Success";
} else {
echo "resized failed";
}
}
What I'm not understanding is that the resize function simply creates the image and detroys the older image files. How it will return the output that it was successful or not? Thanks.
As written your resize function is not returning information on whether it succeeds or fails and, furthermore, it always destroys both images (the two imagedestroy calls). You'll need to rewrite the function to take account of the return codes from imagedestroy and imagejpeg. The documentation for the latter indicates that that function will return a boolean indicating whether it was successful or not.
imagejpeg() returns a boolean FALSE on failure (according to the API; collect its return and use that as the return of your resize() function:
function resize () {
...
$success = imagejpeg($image_new, $img_path, 80);
...
return $success
}
Also, you should probably be passing in the image as a parameter to resize()... it looks like you have a few global variables, which isn't good(1) practise(2)

Categories