Tag Archive for php

Resize Image

<?php
/*
ImageThumb - Creates a thumbnail image from another based on specified sizes

SourceImage - The location of the image in which you want to resize
DestImage - The location to save the thumb, use null if you want to just display to browser
Width - The resized width of the image
Height - The resized height of the image
Type - The image type in which you want to save

Note: The thumb is resized while keeping the aspect ratio, so width and height are not the
absolute width and height.
*/

function ImageThumb($sourceImage, $destImage, $width, $height, $type = "png") {
$type = strtolower($type);
$imageSize = getimagesize($sourceImage);

if($imageSize[0] > $imageSize[1]) {
$newWidth = $width;
$newHeight = $imageSize[1] * ($newWidth / $imageSize[0]);
}
else {
$newHeight = $height;
$newWidth = $imageSize[0] * ($newHeight / $imageSize[1]);
}

switch(image_type_to_mime_type($imageSize[2])) {
case "image/jpeg":
$image = imagecreatefromjpeg($sourceImage);
break;
case "image/gif":
$image = imagecreatefromgif($sourceImage);
break;
case "image/png":
$image = imagecreatefrompng($sourceImage);
break;
default:
$t = image_type_to_mime_type($imageSize[2]);
echo "The file type {$t} is not supported, please use either jpeg, gif, or png";
break;
}

$thumb = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $newWidth, $newHeight, $imageSize[0], $imageSize[1]);

switch($type) {
case "jpg":
case "jpeg":
header("Content-type: image/jpeg");
imagejpeg($thumb, $destImage);
break;
case "gif":
header("Content-type: image/gif");
imagegif($thumb, $destImage);
break;
case "png":
header("Content-type: image/png");
imagepng($thumb, $destImage);
break;
default:
echo "The image type {$type} is not supported, please choose another.";
break;
}

imagedestroy($image);
imagedestroy($thumb);
}
?>

source

Print errors to screen on default php5 in Drupal

<?php
ini_set('error_reporting',          E_ALL);
ini_set('display_errors',           'stdout');
?>

source

php return typoscript object

return $this->cObj->cObjGetSingle($conf['10'], $conf['10.']);

source

php define typoscript from array of associative arrays

//build CONTENT objects
foreach ($items as $item) {

$newItem = ($x+1)*10;
$conf['10.'][$newItem] = 'CONTENT';
$conf['10.'][$newItem.'.'] = $dummyItem;
$conf['10.'][$newItem.'.']['select.']['uidInList'] = $item['uid'];
$conf['10.'][$newItem.'.']['select.']['pidInList'] = $item['pid'];
if($item['lock']) {$conf['10.'][$newItem.'.']['renderObj.']['stdWrap.']['dataWrap'] = $lConf['lockItemWrap'];} // special wrap to make item non-draggable and for providing custom CSS
$x++;

}

source

php while statement associative array from mysql

while($row = mysql_fetch_assoc($res)) {
}

source

Drupal $styles

<?php
// Let's add some extra stylesheets to the queue in the $styles function. Set them to 'module', so they come before style.css.
$styles = drupal_get_css(drupal_add_css(path_to_theme() . '/path/to/style.css', 'module', 'all', TRUE));
$styles = drupal_get_css(drupal_add_css(path_to_theme() . '/path/to/style.css', 'module', 'all', TRUE));
$styles = drupal_get_css(drupal_add_css(path_to_theme() . '/path/to/style.css', 'module', 'all', TRUE));
$styles = drupal_get_css(drupal_add_css(path_to_theme() . '/path/to/style.css', 'module', 'all', TRUE)); ?>

source

PHP Last Day of Current Month

<?php

date("t"); // Number of days in the given month (from php documentation)

?>

source

MySQL Query to HTML Table

function _mysql_result_all($result, $tableFeatures="border='1'",$nodata="&nbsp;") {
$tableDebug .= "<!--Debugging output for SQL query-->

";
echo "<table $tableFeatures>

";
$noFields = mysql_num_fields($result);
echo "<tr>
";
for ($i = 0; $i < $noFields; $i++) {
$field = mysql_field_name($result, $i);
echo "	<th>$field</th>
";
}
while ($r = mysql_fetch_row($result)) {
echo "<tr>
";
foreach ($r as $column) {
echo "	<td>";
if ($column == NULL) {
echo "$nodata";
} else {
echo $column;
}
echo "</td>
";
}
echo "</tr>
";
}
echo "</table>

";
echo "<!--End debug from SQL query-->

";
return $tableDebug;
}

_mysql_result_all($result);

source

Enum to Array / SQL to PHP

function LoadTypeValues($table, $column)
{
global $db;
// Create a SQL Query to get the Columns Type information,
// Open a database connection, execute the query, and retrieve
// the result.

$sql = "show columns from $table like '$column'";

// Get the Type information, Remove "xxx(" from the front
// and ")" from the end.  Split the comma delimited values
// into an array.
$requete = mysql_query($sql, $db);
while($enum = mysql_fetch_array($requete)) {
$enum = $db->f('Type');
$off  = strpos($enum,"(");
$enum = substr($enum, $off+1, strlen($enum)-$off-2);
$values = explode(",",$enum);

// For each value in the array, remove the leading and trailing
// single quotes, convert two single quotes to one. Put the result
// back in the array in the same form as CodeCharge needs.

for( $n = 0; $n < Count($values); $n++) {
$val = substr( $values[$n], 1,strlen($values[$n])-2);
$val = str_replace("''","'",$val);
$values[$n] = array( $val, $val );
}
}
// return the values array to the caller
return $values;
}

source

Clean URL

<?php

$urlArray = explode("/",$_SERVER["REQUEST_URI"]);
$url_what = $urlArray[count($urlArray)-1];

if($url_what == 'home') {
// rather than index.php?act=home, url would be index/home
displayHome();
}elseif($url_what == 'contact'){
// rather than index.php?act=contact,url will be index/contact
displayContact();
}elseif($url_what == 'calendar'){
// rather than index.php?act=calendar,url will be index/calendar
displayCalendar();
}elseif($url_what == 'about'){
// rather than index.php?act=about,url will be index/about
displayAbout();
}else
//if $url_what doesn't exist, display home
writeHome();

?>

source