Friday, 13 July 2012

Cropping Images using PHP


This script will allow you to make a smaller image of a bigger image, but at the same time crop it. This will prevent the image looking stretched and deformed.
We will create this using a class (Sorry using PHP 4 at the moment, but should work on 5)
Firstly lets set up the class.
PHP Code: class cropImage{
//code here
}
Now we need to set up some variables to be used throughout the program.
PHP Code: var $imgSrc,$myImage,$cropHeight,$cropWidth,$x,$y,$thumb;
The variables above will be explained once we use them. Now we need to create the first function. This function will get the image we are going to crop, work out its dimension and then use them dimensions to work out how we are going to crop it.
PHP Code:
function setImage($image)
{
//Your Image
$this->imgSrc = $image;
//getting the image dimensions
list($width, $height) = getimagesize($this->imgSrc);
//create image from the jpeg
this->myImage = imagecreatefromjpeg($this->imgSrc) or die("Error: Cannot find image!");
if($width > $height) $biggestSide = $width; //find biggest length
else $biggestSide = $height;
//The crop size will be half that of the largest side
$cropPercent = .5; // This will zoom in to 50% zoom (crop)
$this->cropWidth   = $biggestSide*$cropPercent;
$this->cropHeight  = $biggestSide*$cropPercent;
//getting the top left coordinate
$this->x = ($width-$this->cropWidth)/2;
$this->y = ($height-$this->cropHeight)/2;
}
Now we actually need to start creating the actual cropped image.
PHP Code: function createThumb()
{
$thumbSize = 250; // will create a 250 x 250 thumb
$this->thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize, $thumbSize, $this->cropWidth, $this->cropHeight);
}
Now all we need to do is render the image out.
PHP Code: function renderImage()
{
header('Content-type: image/jpeg');
imagejpeg($this->thumb);
imagedestroy($this->thumb);
}
Now we just need to create the instance
PHP Code: $image = new cropImage;
$image->setImage($src);
$image->createThumb();
$image->renderImage();
You can use this script to display a thumb of images on a new page, by using the following page.
PHP Code: <img src="thumbcreate.php?src=images/largimg.jpg"> //link to large image

how to debug meta tag in facebook

Test Credit Card Account Numbers


While testing, use only the credit card numbers listed here. Other numbers produce an error.
Expiration Date must be a valid date in the future (use the mmyy format).

Test Credit Card Account Numbers

Credit Card TypeCredit Card Number
American Express378282246310005
American Express371449635398431
American Express Corporate378734493671000
Australian BankCard5610591081018250
Diners Club30569309025904
Diners Club38520000023237
Discover6011111111111117
Discover6011000990139424
JCB3530111333300000
JCB3566002020360505
MasterCard5555555555554444
MasterCard5105105105105100
Visa4111111111111111
Visa4012888888881881
Visa4222222222222
Note : Even though this number has a different character count than the other test numbers, it is the correct and functional number.
Processor-specific Cards
Dankort (PBS)76009244561
Dankort (PBS)5019717010103742
Switch/Solo (Paymentech)6331101999990016

How to validate existing url using Curl


$url = 'http://google.com'; (suppose this is your current url)
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$ch=curl_init();
curl_setopt ($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$page=curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// print_r(curl_getinfo($ch));die;
curl_close($ch);
if($httpcode>200) {
echo 'url exist';  //add whatever the action you want on success of url validation
}
else{
echo 'url not exist';
}

CREATE A PAGE TEMPLATE FOR ANYTHING YOU WANT


Out of the box, Drupal provides the ability to create page templates for different pages/sections. However, its flexibility is relatively limited, in our opinion. This page on drupal.org shows the following page.tpl.php naming conventions and uses:
1. page.tpl.php (global)
2. page-front.tpl.php (front page)
3. page-node-edit.tpl.php (node edit)
4. page-node-[#].tpl.php (specific node)
5. page-node.tpl.php (nodes - global)
6. page-[internal/path].tpl.php (specific path)
This is an interesting list. While it does accommodate for quite a bit, it misses a few seemingly obvious use-cases. Now, this of course depends on your site and its needs. It may suit your purposes just fine. On the off chance it doesn't, however, take a little trip with us down the rabbit hole and find out how to create your own page templates for literally ANYTHING you want.

Page template per content type

One of the things that's missing from the list above is "page-content-type.tpl.php" (e.g. page-blog.tpl.php). What if you want your Blog nodes to use a different structure than your Page nodes?
In your theme's template.php file (create it if it doesn't exist), add the following:
<?php
...
function YOURTHEME_preprocess(&$vars, $hook) {
global $user;
switch($hook) {
case 'page':
// Check to see if the page is a node
if ($node = node_load(arg(1)) && arg(0) == 'node') {
$node = node_load(arg(1));
$types = node_get_types('names');
// Check to see if there is a valid Content Type
if (in_array($node->type, $types)) {
$suggestions[] = "page-node-$node->type";
$vars['template_files'] = array_merge($variables['template_files'], $suggestions);
}
}
}
}
...
?>

Page template for any section

Or, what if you want to add a page-template for an entire section, based on the path (e.g. foo/bar/*)?
<?php
...
function YOURTHEME_preprocess(&$vars, $hook) {
global $user;
switch($hook){
case 'page':
// Check path for /foo/bar/*
if(arg(0) == 'foo' && arg(1) == 'bar' && arg(3)) {
$suggestions[] = "page-foo-bar-section.tpl.php";
}
}
}
...
?>
Just don't forget to rebuild the theme registry if the template.php file is new, or your site won't pick the changes up!
using phptemplate_preprocess_page function
function phptemplate_preprocess_page(&$vars) {
$vars['tabs2'] = menu_secondary_local_tasks();
if(isset($vars['node']) && !empty($vars['node']) && arg(0)=='node' && is_numeric(arg(1))){
$node= $vars['node'];
$vars['template_files'][] = "page-node-".$node->type;
}
// Hook into color.module
if (module_exists('color')) {
_color_page_alter($vars);
}
}
Now put the  page-node-[node-type].tpl.php in your Active theme directory.