简体   繁体   中英

Joomla Image Renedering extension with JFactory access

Is it possible to write an extension for joomla or use some existing plugin similar to jumi to be able to render for instance a png image but with full access to user data (with JFactory)? In other words today it's not a problem to create a php script that renders a public image based on parameters passed. But if I want to access user data and check wether the user logged in or not it becomes a problem.

You will want to create a component (or a part of one) that uses format=raw in the query string. You'll also want to use the JDocument object to set the MIME type to image/png. To do this, create a view in the component called image (or whatever you'd like). Then, instead of creating a view.html.php file, create view.raw.php . Inside that file, add code like this:

<?php

defined( '_JEXEC' ) or die;

jimport( 'joomla.application.component.view');

class YourcomponentnamehereViewImage extends JView
{
    public function display($tpl = null)
    {
        $document =& JFactory::getDocument();
        $document->setMimeEncoding('image/png');

        // your image processing & output here
    }
}

You do not need to create a tmpl folder with default.php as you're not outputting any markup.

I am pretty sure that this is possible. I hate not having anything around me to be absolutely sure, but off the top of my head (and it's been a while!) I think you're looking for 'raw output'.

You should be able to add something on the URL's query string to control the output of the Joomla 'stuff' that is not your code exactly. From what I remember, you need to add something like '&output=raw&no_html=1' to the query string.

Hopefully that will at least get you somewhere...

Thanks to all answers, especially jlleblanc. With the help of them I ended up with a solution using existing well-known extension(s).

All we need is any component that produces php-output. I tried with Jumi, but also it should work with any other.

We create an item in Jumi component (in Jumi Applications Manager ) that produces an image, for example filled rectangle

 <?php
     defined( '_JEXEC' ) or die( 'Restricted access' );

     $im = imagecreatetruecolor(300, 200);

     $bkgcolor = imagecolorallocate($im, 134, 134, 134);
     imagefilledrectangle($im, 0, 0, 300, 200, $bkgcolor);

     $document =& JFactory::getDocument();
     $document->setMimeEncoding('image/png');

     imagepng($im); 
     imagedestroy($im);
 ?>

After adding according to the component rule we have a link to this component item

someoursite.com/index.php?option=com_jumi&fileid=3

Where fileid=3 is the string based on actual id of the item If we just use this url as it is we will get usual Joomla layout with distorted page showing png stream as text (not so visually comfort actually), but a little trick "format=raw" at the end will give us the result that we need.

someoursite.com/index.php?option=com_jumi&fileid=3&format=raw

That's all. After that the correct image is displayed in the browser. Thanks again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM