/** * need GD library (first PHP line WIN: dl("php_gd.dll"); UNIX: dl("gd.so"); * www.boutell.com/gd/ * interval.cz/clanky/php-skript-pro-generovani-galerie-obrazku-2/ * cz.php.net/imagecopyresampled * www.linuxsoft.cz/sw_detail.php?id_item=871 * www.webtip.cz/art/wt_tech_php/liquid_ir.html * php.vrana.cz/zmensovani-obrazku.php * diskuse.jakpsatweb.cz/ * * @param string $file_in Vstupni soubor (mel by existovat) * @param string $file_out Vystupni soubor, null ho jenom zobrazi (taky kdyz nema pravo se zapsat :) * @param int $width Vysledna sirka (maximalni) * @param int $height Vysledna vyska (maximalni) * @param bool $crop Orez (true, obrazek bude presne tak velky), jinak jenom Resample (udane maximalni rozmery) * @param int $type_out IMAGETYPE_type vystupniho obrazku * @return bool Chyba kdyz vrati false */ function imageMagic($file_in, $file_out = null, $width = null, $height = null, $crop = null, $type_out = null, $watermarkParams = array(), $frontUpload = 0) { $fileWatermark = ''; // While front upload we don't display the process page if ($frontUpload == 0) { $stopText = PhocaGalleryHelper::displayStopThumbnailsCreating(); echo $stopText; } $memory = 8; $memoryLimitChanged = 0; $memory = (int) ini_get('memory_limit'); if ($memory == 0) { $memory = 8; } if ($file_in !== '' && file_exists($file_in)) { //array of width, height, IMAGETYPE, "height=x width=x" (string) list($w, $h, $type) = GetImageSize($file_in); if ($w > 0 && $h > 0) { // we got the info from GetImageSize // size of the image if ($width == null || $width == 0) { // no width added $width = $w; } else { if ($height == null || $height == 0) { // no height, adding the same as width $height = $width; } } if ($height == null || $height == 0) { // no height, no width $height = $h; } // miniaturizing if (!$crop) { // new size - nw, nh (new width/height) $scale = $width / $w < $height / $h ? $width / $w : $height / $h; // smaller rate $src = array(0, 0, $w, $h); $dst = array(0, 0, floor($w * $scale), floor($h * $scale)); } else { // will be cropped $scale = $width / $w > $height / $h ? $width / $w : $height / $h; // greater rate $newW = $width / $scale; // check the size of in file $newH = $height / $scale; // which side is larger (rounding error) if ($w - $newW > $h - $newH) { $src = array(floor(($w - $newW) / 2), 0, floor($newW), $h); } else { $src = array(0, floor(($h - $newH) / 2), $w, floor($newH)); } $dst = array(0, 0, floor($width), floor($height)); } // Watermark if (!empty($watermarkParams) && $watermarkParams['create'] == 1) { $thumbnailSmall = false; $thumbnailMedium = false; $thumbnailLarge = false; $thumbnailMedium = preg_match("/phoca_thumb_m_/i", $file_out); $thumbnailLarge = preg_match("/phoca_thumb_l_/i", $file_out); $fileName = PhocaGalleryHelper::getTitleFromFilenameWithExt($file_in); // Which Watermark will be used if ($thumbnailMedium) { $fileWatermark = str_replace($fileName, 'watermark-medium.png', $file_in); } else { if ($thumbnailLarge) { $fileWatermark = str_replace($fileName, 'watermark-large.png', $file_in); } else { $fileWatermark = ''; } } if (!file_exists($fileWatermark)) { $fileWatermark = ''; } if ($fileWatermark != '') { list($wW, $hW, $typeW) = GetImageSize($fileWatermark); switch ($watermarkParams['x']) { case 'left': $locationX = 0; break; case 'right': $locationX = $dst[2] - $wW; break; case 'center': default: $locationX = $dst[2] / 2 - $wW / 2; break; } switch ($watermarkParams['y']) { case 'top': $locationY = 0; break; case 'bottom': $locationY = $dst[3] - $hW; break; case 'middle': default: $locationY = $dst[3] / 2 - $hW / 2; break; } } } else { $fileWatermark = ''; } } if ($memory < 50) { ini_set('memory_limit', '50M'); $memoryLimitChanged = 1; } // Resampling // in file // Watemark if ($fileWatermark != '') { if (!function_exists('ImageCreateFromPNG')) { return 'ErrorNoPNGFunction'; } $waterImage1 = ImageCreateFromPNG($fileWatermark); } // End Watermark switch ($type) { case IMAGETYPE_JPEG: if (!function_exists('ImageCreateFromJPEG')) { return 'ErrorNoJPGFunction'; } $image1 = ImageCreateFromJPEG($file_in); break; case IMAGETYPE_PNG: if (!function_exists('ImageCreateFromPNG')) { return 'ErrorNoPNGFunction'; } $image1 = ImageCreateFromPNG($file_in); break; case IMAGETYPE_GIF: if (!function_exists('ImageCreateFromGIF')) { return 'ErrorNoGIFFunction'; } $image1 = ImageCreateFromGIF($file_in); break; case IMAGETYPE_WBMP: if (!function_exists('ImageCreateFromWBMP')) { return 'ErrorNoWBMPFunction'; } $image1 = ImageCreateFromWBMP($file_in); break; default: return 'ErrorNotSupportedImage'; break; } if ($image1) { // protection against invalid image dimensions /*foreach ($dst as $kdst =>$vdst) { if ($dst[$kdst] == 0 ) { $dst[$kdst] = 1; } } */ $image2 = @ImageCreateTruecolor($dst[2], $dst[3]); if (!$image2) { return 'ErrorNoImageCreateTruecolor'; } switch ($type) { case IMAGETYPE_PNG: //imagealphablending($image1, false); @imagealphablending($image2, false); //imagesavealpha($image1, true); @imagesavealpha($image2, true); break; } ImageCopyResampled($image2, $image1, $dst[0], $dst[1], $src[0], $src[1], $dst[2], $dst[3], $src[2], $src[3]); // Watermark if ($fileWatermark != '') { ImageCopy($image2, $waterImage1, $locationX, $locationY, 0, 0, $wW, $hW); } // End Watermark // display the image if ($file_out == null) { header("Content-type: " . image_type_to_mime_type($type_out)); } // out file if ($type_out == null) { // no bitmap $type_out = $type == IMAGETYPE_WBMP ? IMAGETYPE_PNG : $type; } switch ($type_out) { case IMAGETYPE_JPEG: if (!function_exists('ImageJPEG')) { return 'ErrorNoJPGFunction'; } if (!@ImageJPEG($image2, $file_out, 85)) { return 'ErrorWriteFile'; } break; case IMAGETYPE_PNG: if (!function_exists('ImagePNG')) { return 'ErrorNoPNGFunction'; } if (!@ImagePNG($image2, $file_out)) { return 'ErrorWriteFile'; } break; case IMAGETYPE_GIF: if (!function_exists('ImageGIF')) { return 'ErrorNoGIFFunction'; } if (!@ImageGIF($image2, $file_out)) { return 'ErrorWriteFile'; } break; default: return 'ErrorNotSupportedImage'; break; } // free memory ImageDestroy($image1); ImageDestroy($image2); if (isset($waterImage1)) { ImageDestroy($waterImage1); } if ($memoryLimitChanged == 1) { $memoryString = $memory . 'M'; ini_set('memory_limit', $memoryString); } return 'Success'; // Success } else { return 'Error1'; } if ($memoryLimitChanged == 1) { $memoryString = $memory . 'M'; ini_set('memory_limit', $memoryString); } } return 'Error2'; }