It is man's ability to remember that sets us apart, we are the only species concerned with the past. Our memories give us voice, to bear witness to history so that others might learn, so they might celebrate our triumph and be warned of our failures ...

File / Image not getting uploaded from Internet Explorer

Filed Under (PHP) by Abhishek Jain on 25-11-2010

Tagged Under : , , ,

At MyShaadi.in we provide our users with a feature to upload photos in their wedding site.

However our users have been complaining about not being able to upload photos when using Internet Explorer, though the

same image could be uploaded successfully when using Firefox or Chrome . . any non-IE browser for that matter … :)

Solution

We check for file type when a user uploads an image .. to ensure only valid image types are uploaded.

In case of non – ie browsers .. for jpeg image file type is “image/jpeg” or “image/jpg” .. for png .. “image/png” ..

However as usual IE decided to not go by the usual way .. and instead of the returning the usual  .. returns

“image/pjpeg” and “image/x-png” .. and thats where the culprit was … added these two to the list of valid image

types and things started working again .. :)

Below is the code we use to check valid image types ..

function isValidImage($img_type) {
// allowed image types ..
$allowed_img_types = array("JPG","gif","jpg","jpeg","bmp","tif","png","image/jpeg","image/gif",
"image/bmp","image/jpg","image/png","image/x-png","image/pjpeg");
return in_array($img_type, $allowed_img_types);
}

How to create transparent images with Imagick

Filed Under (PHP) by Abhishek Jain on 11-05-2010

Tagged Under : , , , ,

<?php
class ImageCreator {
    var $ttf_file = "English.ttf";

    function createPng($text,$dest_file) {
        try {
            /*** a new Imagick object ***/
            $im = new Imagick();

            /*** a new draw object ***/
            $draw = new ImagickDraw();

            /*** set the font ***/
            $draw->setFont($this->ttf_file);

            /*** set the font color ***/
            $draw->setFillColor(new ImagickPixel("#d4d4d4"));

            /*** set the font size ***/
            $draw->setFontSize( 40 );

            /*** set the box color ***/
            $pixel = new ImagickPixel( 'transparent' );

            /*** get the font info ***/
            $font_info = $im->queryFontMetrics($draw, $text );

            /*** the width ***/
            $width = $font_info['textWidth']+10;

            /*** the height ***/
            $height = $font_info['textHeight'];

            /*** a new image with the dynamic sizes ***/
            $im->newImage($width, $height, $pixel);

            /*** annotate the text on the image ***/
            $im->annotateImage($draw, 0,27, 0, $text);

            /*** set the image format ***/
            $im->setImageFormat('png');

            /*** write image to disk ***/
            $im->writeImage($dest_file);
        } catch (Exception $e) {
            $this->log($e->getMessage());
        }
    }
}

Executing Cron Job in CakePHP

Filed Under (PHP) by Abhishek Jain on 29-03-2010

Tagged Under : , ,

After going through a number of ways to execute a cron job in CakePHP ..

The option below seemed to be the simplest and easiest to execute.
However this would work only on a linux / unix machine and requires lynx be installed in your machine.

lynx –dump http://www.your_domain.com/controller/action/ >/dev/null

So for a cron job:

*/5 * * * * lynx –dump http://www.your_domain.com/controller/action/ > /dev/null

Useful links related to Lynx:

http://en.wikipedia.org/wiki/Lynx_(web_browser)

http://tips.webdesign10.com/general/lynx-browser

Shell for PHP

Filed Under (PHP) by Abhishek Jain on 03-03-2010

Tagged Under : ,

Features

  • tab-completion (if readline() support is compiled into php)
  • handles FATAL errors (like calling a undefined function)
  • inline help
  • autoload() is enabled by default
  • works on all php platforms (shell wrappers for Unix and Windows)

Complete Article : http://jan.kneschke.de/projects/php-shell/

Passing Parameters Into Your Layouts in CakePHP

Filed Under (PHP) by Abhishek Jain on 07-09-2009

Interestingly enough, the developers over at CakePHP incorporated this little interesting tidbit: any variable set for your view with a name ending in “_for_layout” automatically gets passed into the layout.

Here’s a little example:


//in my controller
$this->set(‘var_for_layout’,$value);

Source:

http://kushaura.com/blog/view/name:Passing-Parameters-Into-Your-Layouts-in-CakePHP

URL Rewriting in Apache

Filed Under (PHP) by Abhishek Jain on 18-07-2009

What if you want a feature like twitter in your site .. twitter.com/username .. redirects you to the profile page of the user.

Usually Apache would consider “username” to be a folder under the root folder and try opening its index page. Thats not what we intend to do .. create a folder and its index page for each user .. doesnt make sense.

That’s where URL rewriting in Apache comes to the rescue :) and turns out its pretty simple to achieve this.

Assumption: Basic knowledge of Regular Expressions.

The Apache server’s mod_rewrite module gives you the ability to transparently redirect one URL to another,

with / without the user’s knowledge.

Lets see how do we go about it.

Some servers will not have » mod_rewrite enabled by default. As long as the » module is present in the installation,you can enable it simply by starting a .htaccess file with the command


RewriteEngine

Put this .htaccess file in your root so that rewriting is enabled throughout your site. You only need to write this line once per .htaccess file.

This is how the rule will look like in the .htaccess file


RewriteEngine on

RewriteRule ^/([A-Z][a-z][0-9][0-9])*$ /users/view_profile.php?username=$1 

This rule wouldn’t change the url in the address bar, keeping the redirection hidden from the user. In case you want that add a [R] at the end of the rule


RewriteEngine on

RewriteRule ^/([A-Z][a-z][0-9][0-9])*$ /users/view_profile.php?username=$1 [R]

Note that it is also possible to chain rules and they will be executed in the same sequence as mentioned in .htaccess file.

One of the things to note here is that there would be cases where “string” in  “xyz.com/string” would be a valid folder name under the root directory and not “username”. These cases would have to be handled in the view_profile.php page. There could be multiple ways to do it, which i will leave upto you to decide.

Another situation where the url rewriting thing can be used is when you have moved a page and the user is not aware of the change. You dont want to show him a 404 page (that scares people :) ). Use this to redirect him to the new page.

RewriteEngine on
RewriteRule ^old\.php$ new.php

Named Routing in CakePHP

Filed Under (PHP) by Abhishek Jain on 31-05-2009

Tagged Under : ,

Named routing – dunno if this term even exists in CakePHP .. :)

What we are trying to achieve here is to redirect to a user profile based on the username entered.

e.g www.example.com/username should redirect him to the user’s profile.

To do this the first thing that we will have to do is change the route.php file of CakePHP ( you can find it in /project_name/app/config).


// route.php

Router::connect('/:username', array('controller' => 'users', 'action' => 'route_to_profile'));

//In case you want the username to follow some pattern add this

Router::connect('/:username', array('controller' => 'users', 'action' => 'route_to_profile'), array('username' => "[a-z0-9]+"));

In the controller page (users_controller.php) add the following function to redirect to users profile page


//users_controller.php

function route_to_profile(){

$username = $this->params['username'];

// search for the id of the user with the given username

$conditions = array('User.username'=>$username);

$fields = array('User.id');

$recursive = -1;

$user = $this->User->find('first',array('conditions'=>$conditions,'fields'=>$fields,'recursive'=>$recursive));

// if found redirect to appropriate page, else redirect to error page

if($user != null){

$user_id = $user['User']['id'];

$this->redirect(array('controller'=>'users','action'=>'view',$user_id),null,true);

}else{

$this->redirect(array('controller'=>'users','action'=>'error'),null,true);

}

}

One problem with the above code is that when we use html helper to generate link to the index action of a controller

we use,


$html->link(__(' View_Text', true),array('controller'=>'controller_name','action'=>'index'));

The link that will be generated by the above function would be


http://www.example.com/controller_name

instead of


http://www.example.com/controller_name/index

The former link will be misinterpreted by the router, because it will consider controller_name as username and will try redirecting to the view page, which is not what we intend to.

Am still looking for solution to this problem, one temporary way could be to generate the index link using the following code


$html->link(__(' View_Text', true),'/controller_name/index');

which will generate the following html ..


http://www.example.com/controller_name/index
Subscribe to Rss Feed : Rss