function captcha($width, $height, $code) { $font = "./font/ChalkboardBold.ttf"; $font_size = 17; $image = imagecreate($width, $height); $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } $x = 3; $y = 20; imagettftext($image, $font_size, rand(-45, 45), $x, $y + rand() % 16, $text_color, $font, $code[0]); imagettftext($image, $font_size, rand(-45, 45), $x + 23, $y + rand() % 16, $text_color, $font, $code[1]); imagettftext($image, $font_size, rand(-45, 45), $x + 46, $y + rand() % 16, $text_color, $font, $code[2]); imagettftext($image, $font_size, rand(-45, 45), $x + 69, $y + rand() % 16, $text_color, $font, $code[3]); imagettftext($image, $font_size, rand(-45, 45), $x + 92, $y + rand() % 16, $text_color, $font, $code[4]); imagettftext($image, $font_size, rand(-45, 45), $x + 115, $y + rand() % 16, $text_color, $font, $code[5]); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); }
public function cropnsave($id) { $basepath = app_path() . '/storage/uploads/'; $jpeg_quality = 90; $src = $basepath . 'resize_' . $id; if (ImageModel::getImgTypeByExtension($id) == ImageModel::IMGTYPE_PNG) { $img_r = imagecreatefrompng($src); } else { $img_r = imagecreatefromjpeg($src); } $dst_r = ImageCreateTrueColor(Input::get('w'), Input::get('h')); imagecopyresampled($dst_r, $img_r, 0, 0, Input::get('x'), Input::get('y'), Input::get('w'), Input::get('h'), Input::get('w'), Input::get('h')); $filename = $basepath . 'crop_' . $id; imagejpeg($dst_r, $filename, $jpeg_quality); $image = ImageModel::createImageModel('crop_' . $id); try { $session = Session::get('user'); $userService = new SoapClient(Config::get('wsdl.user')); $currentUser = $userService->getUserById(array("userId" => $session['data']->id)); $currentUser = $currentUser->user; $currentUser->avatar = $image; $result = $userService->updateUser(array("user" => $currentUser)); // Cleanup File::delete($src); File::delete($filename); File::delete($basepath . $id); return Response::json($result->complete); } catch (Exception $ex) { error_log($ex); } }
public function filter($value) { if (!file_exists($value)) { throw new Zend_Filter_Exception('Image does not exist: ' . $value); } $image = imagecreatefromstring(file_get_contents($value)); if (false === $image) { throw new Zend_Filter_Exception('Can\'t load image: ' . $value); } // find ratio to scale down to // TODO: pass 600 as parameter in the future $origWidth = imagesx($image); $origHeight = imagesy($image); $ratio = max($origWidth, $origHeight) / 600; if ($ratio > 1) { // img too big! create a scaled down image $newWidth = round($origWidth / $ratio); $newHeight = round($origHeight / $ratio); $resized = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($resized, $image, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight); // determine type and store to disk $explodeResult = explode(".", $value); $type = strtolower($explodeResult[count($explodeResult) - 1]); $writeFunc = 'image' . $type; if ($type == 'jpeg' || $type == 'jpg') { imagejpeg($resized, $value, 100); } else { $writeFunc($resized, $value); } } return $value; }
public static function image($item, $path, $filename) { $image_tempname1 = $_FILES[$item]['name']; $ImageName = $path . $image_tempname1; $newfilename = $path . $filename; if (move_uploaded_file($_FILES[$item]['tmp_name'], $ImageName)) { list($width, $height, $type, $attr) = getimagesize($ImageName); if ($type == 2) { rename($ImageName, $newfilename); } else { if ($type == 1) { $image_old = imagecreatefromgif($ImageName); } elseif ($type == 3) { $image_old = imagecreatefrompng($ImageName); } $image_jpg = imagecreatetruecolor($width, $height); imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $width, $height, $width, $height); imagejpeg($image_jpg, $newfilename); imagedestroy($image_old); imagedestroy($image_jpg); } return 1; } else { return 0; } }
function create_pic($upfile, $new_path, $width) { $quality = 100; $image_path = $upfile; $image_info = getimagesize($image_path); $exname = ''; //1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF switch ($image_info[2]) { case 1: @($image = imagecreatefromgif($image_path)); $exname = 'gif'; break; case 2: @($image = imagecreatefromjpeg($image_path)); $exname = 'jpg'; break; case 3: @($image = imagecreatefrompng($image_path)); $exname = 'png'; break; case 6: @($image = imagecreatefromwbmp($image_path)); $exname = 'wbmp'; break; } $T_width = $image_info[0]; $T_height = $image_info[1]; if (!empty($image)) { $image_x = imagesx($image); $image_y = imagesy($image); } else { return FALSE; } @chmod($new_path, 0777); if ($image_x > $width) { $x = $width; $y = intval($x * $image_y / $image_x); } else { @copy($image_path, $new_path . '.' . $exname); return $exname; } $newimage = imagecreatetruecolor($x, $y); imagecopyresampled($newimage, $image, 0, 0, 0, 0, $x, $y, $image_x, $image_y); switch ($image_info[2]) { case 1: imagegif($newimage, $new_path . '.gif', $quality); break; case 2: imagejpeg($newimage, $new_path . '.jpg', $quality); break; case 3: imagepng($newimage, $new_path . '.png', $quality); break; case 6: imagewbmp($newimage, $new_path . '.wbmp', $quality); break; } imagedestroy($newimage); return $exname; }
function saveinfo() { $juser =& JFactory::getUser(); if ($juser->guest) { $error = 'Bạn phải đăng nhập để thực hiện chức năng này'; $this->setError($error); return false; } $post = JRequest::get('post'); $data = array(); $data['user_id'] = $juser->id; $data['couple_name'] = $post['couple_name']; $data['address'] = $post['address']; $data['country'] = $post['country']; if (isset($_FILES['avatar']) && $_FILES['avatar']['error'] == 0) { require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers' . DS . 'thumb.php'; $p = manThumb::resize($_FILES['avatar']['tmp_name'], 96, 96); $name = 'avatar_' . $juser->id . '.jpg'; $file = JPATH_ROOT . DS . 'images' . DS . 'wedding' . DS . 'avatar' . DS . $name; imagejpeg($p, $file, 90); imagedestroy($p); $data['avatar'] = 'images/wedding/avatar/' . $name; } $row =& $this->getTable('users'); if (!$row->bind($data)) { $this->setError($this->_db->getErrorMsg()); return false; } if (!$row->store()) { $this->setError($this->_db->getErrorMsg()); return false; } return true; }
public function index() { ob_clean(); $image_handle = imagecreatetruecolor(150, 60); $white = imagecolorallocate($image_handle, 255, 255, 255); $rndm = imagecolorallocate($image_handle, rand(64, 192), rand(64, 192), rand(64, 192)); imagefill($image_handle, 0, 0, $white); $fontName = PUBLICPATH . "/fonts/elephant.ttf"; $myX = 15; $myY = 30; $angle = 0; for ($x = 0; $x <= 100; $x++) { $myX = rand(1, 148); $myY = rand(1, 58); imageline($image_handle, $myX, $myY, $myX + rand(-5, 5), $myY + rand(-5, 5), $rndm); } $myCryptBase = tep_create_random_value(50, 'digits'); $secure_image_hash_string = ""; for ($x = 0; $x <= 4; $x++) { $dark = imagecolorallocate($image_handle, rand(5, 128), rand(5, 128), rand(5, 128)); $capChar = substr($myCryptBase, rand(1, 35), 1); $secure_image_hash_string .= $capChar; $fs = rand(20, 26); $myX = 15 + ($x * 28 + rand(-5, 5)); $myY = rand($fs + 2, 55); $angle = rand(-30, 30); ImageTTFText($image_handle, $fs, $angle, $myX, $myY, $dark, $fontName, $capChar); } $this->session->set_userdata('secure_image_hash_string', $secure_image_hash_string); header("Content-type: image/jpeg"); imagejpeg($image_handle, "", 95); imagedestroy($image_handle); die; }
function create_thumbnail($filename) { $percent = 0.1; list($width, $height) = getimagesize("/var/www/amberandbrice.com/workspace/images/" . $filename); $newwidth = $width * $percent; $newheight = $height * $percent; $thumb = imagecreatetruecolor($newwidth, $newheight); $ext = pathinfo($filename)['extension']; echo "<h1>{$ext}</h1>"; switch ($ext) { case 'jpg': case 'jpeg': case 'JPG': $source = imagecreatefromjpeg("images/" . $filename); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($thumb, "./thumbs/" . $filename); break; case 'gif': $source = imagecreatefromgif("images/" . $filename); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($thumb, "./thumbs/" . $filename); break; case 'png': $source = imagecreatefrompng("images/" . $filename); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($thumb, "./thumbs/" . $filename); break; default: die("image extension cannot be determined"); } }
function resize_image($pathToImages, $pathToThumbs, $thumbWidth) { // open the directory $dir = opendir($pathToImages); // loop through it, looking for any/all JPG files: while (false !== ($fname = readdir($dir))) { // parse path for the extension $info = pathinfo($pathToImages . $fname); // continue only if this is a JPEG image if (strtolower($info['extension']) == 'jpg' && $fname != "thumb.jpg") { // load image and get image size $img = imagecreatefromjpeg("{$pathToImages}" . "/" . "{$fname}"); $width = imagesx($img); $height = imagesy($img); // calculate thumbnail size $new_width = $thumbWidth; $new_height = floor($height * ($thumbWidth / $width)); // create a new temporary image $tmp_img = imagecreatetruecolor($new_width, $new_height); // copy and resize old image into new image imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // save thumbnail into a file imagejpeg($tmp_img, "{$pathToThumbs}" . "/" . "{$fname}"); echo "Se redujo la imagen {$fname} <br />"; //echo "$pathToThumbs" . "/" . "$fname<br/>"; } } // close the directory closedir($dir); }
/** * Return captcha image * @param number $maxChar */ public function generate($maxChar = 4) { // $characters = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'; $characters = 'ABCDEFGHKMNPQRST'; $captchaText = ''; for ($i = 0; $i < $maxChar; $i++) { $captchaText .= $characters[rand(0, strlen($characters) - 1)]; } strtoupper(substr(md5(microtime()), 0, 7)); \Session::put('captchaHash', \Hash::make($captchaText)); $image = imagecreate(30 * $maxChar, 35); $background = imagecolorallocatealpha($image, 255, 255, 255, 1); $textColor = imagecolorallocatealpha($image, 206, 33, 39, 1); $x = 5; $y = 20; $angle = 0; for ($i = 0; $i < 7; $i++) { $fontSize = 16; $text = substr($captchaText, $i, 1); imagettftext($image, $fontSize, $angle, $x, $y, $textColor, public_path('/fonts/LibreBaskerville/librebaskerville-regular.ttf'), $text); $x = $x + 17 + mt_rand(1, 10); $y = mt_rand(18, 25); $angle = mt_rand(0, 20); } header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); header('Pragma: no-cache'); header('Content-type: image/jpeg'); imagejpeg($image, null, 100); imagedestroy($image); }
function resizer($filename, $copypath, $MaxWidth, $MaxHeight) { //cesta k souboru, ktery chcete zmensit cesta, kam zmenseny soubor ulozit maximalni sirka zmenseneho obrazku //maximalni vyska zmenseneho obrazku //zjistime puvodni velikost obrazku list($OrigWidth, $OrigHeight) = getimagesize($filename); //hodnota 0 v parametrech MaxWidth resp. MaxHeight znamena, ze sirka resp. vyska vysledku muze byt libovolna if ($MaxWidth == 0) { $MaxWidth = $OrigWidth; } if ($MaxHeight == 0) { $MaxHeight = $OrigHeight; } //nyni vypocitam pomer zmenseni $pw = $OrigWidth / $MaxWidth; $ph = $OrigHeight / $MaxHeight; if ($pw > $ph) { $p = $pw; } else { $p = $ph; } if ($p < 1) { $p = 1; } //v p ted mame pomer pro zmenseni vypocitame vysku a sirku zmenseneho obrazku $NewWidth = (int) $OrigWidth / $p; $NewHeight = (int) $OrigHeight / $p; //vytvorime novy obrazek pozadovane vysky a sirky $image_p = imagecreatetruecolor($NewWidth, $NewHeight); //otevreme puvodni obrazek se souboru $image = imagecreatefromjpeg($filename); //a okopirujeme zmenseny puvodni obrazek do noveho imagecopyresampled($image_p, $image, 0, 0, 0, 0, $NewWidth, $NewHeight, $OrigWidth, $OrigHeight); //a ulozime imagejpeg($image_p, $copypath, 100); }
/** * Render the gradient to an image **/ public function render($width, $height, $format = 'png', $filename = null) { $image = $this->render_gd($width, $height); switch ($format) { case 'jpg': case 'jpeg': case 'jpe': if (empty($filename)) { header('Content-Type: image/jpeg'); } imagejpeg($image, $filename, 90); break; case 'gif': if (empty($filename)) { header('Content-Type: image/gif'); } imagegif($image, $filename); break; case 'png': default: if (empty($filename)) { header('Content-Type: image/png'); } imagepng($image, $filename, 9); break; } imagedestroy($image); }
function resizeThumbnailImage($thumb, $image, $width, $height, $start_width, $start_height, $scale) { $newImageWidth = ceil($width * $scale); $newImageHeight = ceil($height * $scale); $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight); $ext = strtolower(substr(basename($image), strrpos(basename($image), '.') + 1)); $source = ''; if ($ext == 'png') { $source = imagecreatefrompng($image); } elseif ($ext == 'jpg' || $ext == 'jpeg') { $source = imagecreatefromjpeg($image); } elseif ($ext == 'gif') { $source = imagecreatefromgif($image); } imagecopyresampled($newImage, $source, 0, 0, $start_width, $start_height, $newImageWidth, $newImageHeight, $width, $height); $ext = strtolower(substr(basename($thumb), strrpos(basename($thumb), '.') + 1)); if ($ext == 'png') { $result = imagepng($newImage, $thumb, 0); } elseif ($ext == 'jpg' || $ext == 'jpeg') { $result = imagejpeg($newImage, $thumb, 90); } elseif ($ext == 'gif') { $result = imagegif($newImage, $thumb); } if (!$result) { return false; } chmod($thumb, 0664); return $thumb; }
function convertImage($originalImage, $outputImage, $quality = 100) { $myfile = fopen($outputImage, "w"); // jpg, png, gif or bmp? $exploded = explode('.', $originalImage); $ext = $exploded[count($exploded) - 1]; if (preg_match('/jpg|jpeg/i', $ext)) { $imageTmp = imagecreatefromjpeg($originalImage); } else { if (preg_match('/png/i', $ext)) { $imageTmp = imagecreatefrompng($originalImage); } else { if (preg_match('/gif/i', $ext)) { $imageTmp = imagecreatefromgif($originalImage); } else { if (preg_match('/bmp/i', $ext)) { $imageTmp = imagecreatefrombmp($originalImage); } else { return 0; } } } } imagejpeg($imageTmp, $outputImage, $quality); imagedestroy($imageTmp); return $outputImage; }
function resize($photo_src, $width, $name) { $parametr = getimagesize($photo_src); list($width_orig, $height_orig) = getimagesize($photo_src); $ratio_orig = $width_orig / $height_orig; $new_width = $width; $new_height = $width / $ratio_orig; $newpic = imagecreatetruecolor($new_width, $new_height); $col2 = imagecolorallocate($newpic, 255, 255, 255); imagefilledrectangle($newpic, 0, 0, $new_width, $new_width, $col2); switch ($parametr[2]) { case 1: $image = imagecreatefromgif($photo_src); break; case 2: $image = imagecreatefromjpeg($photo_src); break; case 3: $image = imagecreatefrompng($photo_src); break; } imagecopyresampled($newpic, $image, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); imagejpeg($newpic, $name, 100); return true; }
/** * Save the image as the image type the original image was * * @param String[type] $savePath - The path to store the new image * @param string $imageQuality - The qulaity level of image to create * * @return Saves the image */ public function saveImage($savePath, $imageQuality = "100", $download = false) { switch ($this->ext) { case 'image/jpg': case 'image/jpeg': // Check PHP supports this file type if (imagetypes() & IMG_JPG) { imagejpeg($this->newImage, $savePath, $imageQuality); } break; case 'image/gif': // Check PHP supports this file type if (imagetypes() & IMG_GIF) { imagegif($this->newImage, $savePath); } break; case 'image/png': $invertScaleQuality = 9 - round($imageQuality / 100 * 9); // Check PHP supports this file type if (imagetypes() & IMG_PNG) { imagepng($this->newImage, $savePath, $invertScaleQuality); } break; } if ($download) { header('Content-Description: File Transfer'); header("Content-type: application/octet-stream"); header("Content-disposition: attachment; filename= " . $savePath . ""); readfile($savePath); } imagedestroy($this->newImage); }
function upload($tmp, $name, $nome, $larguraP, $pasta) { $ext = strtolower(end(explode('.', $name))); if ($ext == 'jpg') { $img = imagecreatefromjpeg($tmp); } elseif ($ext == 'gif') { $img = imagecreatefromgif($tmp); } else { $img = imagecreatefrompng($tmp); } $x = imagesx($img); $y = imagesy($img); $largura = $x > $larguraP ? $larguraP : $x; $altura = $largura * $y / $x; if ($altura > $larguraP) { $altura = $larguraP; $largura = $altura * $x / $y; } $nova = imagecreatetruecolor($largura, $altura); imagecopyresampled($nova, $img, 0, 0, 0, 0, $largura, $altura, $x, $y); imagejpeg($nova, "{$pasta}/{$nome}"); imagedestroy($img); imagedestroy($nova); return $nome; }
public function testUploadFile() { $album = $this->Album->init(); $tmp_file = "/tmp/111111111.jpg"; # Generate custom image to test upload $im = imagecreatetruecolor(120, 20); $text_color = imagecolorallocate($im, 233, 14, 91); imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color); imagejpeg($im, $tmp_file); # Generate path to save file $file_path = $this->Picture->generateFilePath($album['Album']['id'], 'picturetests.jpg'); $file = new File($file_path); # Get resize options $resize_attrs = $this->Picture->getResizeToSize(); # Upload and save $should_return_3 = $this->Picture->uploadFile($file_path, $album['Album']['id'], 'samplepicture.jpg', $tmp_file, $resize_attrs['width'], $resize_attrs['height'], $resize_attrs['action'], true); # File was saved? $this->assertEqual(3, $should_return_3); # File was uploaded? $this->assertTrue($file->exists()); # Should rise exception try { $this->Picture->uploadFile($file_path, null, 'samplepicture.jpg', $tmp_file, $resize_attrs['width'], $resize_attrs['height'], $resize_attrs['action'], true); } catch (ForbiddenException $e) { $this->assertEqual($e->getMessage(), "The album ID is required"); } }
function CaptchaSecurityImages($width = '120', $height = '40', $characters = '6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.5; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 20, 20, 20); $text_color = imagecolorallocate($image, 230, 197, 89); $noise_color = imagecolorallocate($image, 0, 0, 0); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ /*for( $i=0; $i<($width*$height)/150; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); }*/ /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $_SESSION['security_code'] = $code; }
function background() { $this->im = imagecreatetruecolor($this->width, $this->height); $backgrounds = $c = array(); if (!$this->background || !$backgrounds) { for ($i = 0; $i < 3; $i++) { $start[$i] = mt_rand(200, 255); $end[$i] = mt_rand(100, 150); $step[$i] = ($end[$i] - $start[$i]) / $this->width; $c[$i] = $start[$i]; } for ($i = 0; $i < $this->width; $i++) { $color = imagecolorallocate($this->im, $c[0], $c[1], $c[2]); imageline($this->im, $i, 0, $i, $this->height, $color); $c[0] += $step[0]; $c[1] += $step[1]; $c[2] += $step[2]; } $c[0] -= 20; $c[1] -= 20; $c[2] -= 20; } ob_start(); if (function_exists('imagepng')) { imagepng($this->im); } else { imagejpeg($this->im, '', 100); } imagedestroy($this->im); $bgcontent = ob_get_contents(); ob_end_clean(); $this->fontcolor = $c; return $bgcontent; }
/** * Create thumbnail image by php using the GD Library * * @since 1.0.0 */ public static function createThumb($source_image, $destination_image_url, $width, $height, $quality, $crop = null, $prefix = null, $checksize = null) { //Set image ratio list($w, $h) = getimagesize($source_image); // resize if ($crop === true) { if ($checksize && ($w < $width or $h < $height)) { $width = $w + 1; $height = $h + 1; $x = 0; } else { $ratio = max($width / $w, $height / $h); $h = $height / $ratio; $x = ($w - $width / $ratio) / 2; $w = $width / $ratio; } } else { if ($checksize && ($w < $width or $h < $height)) { $width = $w; $height = $h; $x = 0; } else { $ratio = min($width / $w, $height / $h); $width = $w * $ratio; $height = $h * $ratio; $x = 0; } } if (preg_match("/.jpg/i", "{$source_image}") or preg_match("/.jpeg/i", "{$source_image}")) { //JPEG type thumbnail $destImage = imagecreatetruecolor($width, $height); $sourceImage = imagecreatefromjpeg($source_image); imagecopyresampled($destImage, $sourceImage, 0, 0, $x, 0, $width, $height, $w, $h); imagejpeg($destImage, $destination_image_url, $quality); imagedestroy($destImage); } elseif (preg_match("/.png/i", "{$source_image}")) { //PNG type thumbnail $destImage = imagecreatetruecolor($width, $height); $sourceImage = imagecreatefrompng($source_image); imagealphablending($destImage, false); imagecopyresampled($destImage, $sourceImage, 0, 0, $x, 0, $width, $height, $w, $h); imagesavealpha($destImage, true); imagepng($destImage, $destination_image_url); } elseif (preg_match("/.gif/i", "{$source_image}")) { //GIF type thumbnail $destImage = imagecreatetruecolor($width, $height); $sourceImage = imagecreatefromgif($source_image); $bgc = imagecolorallocate($destImage, 255, 255, 255); imagefilledrectangle($destImage, 0, 0, $width, $height, $bgc); imagecopyresampled($destImage, $sourceImage, 0, 0, $x, 0, $width, $height, $w, $h); if (function_exists('imagegif')) { // Pour GIF header('Content-Type: image/gif'); imagegif($destImage, $destination_image_url, $quality); } imagedestroy($destImage); } else { echo 'unable to load image source'; } }
public static function create() { $image = imagecreatetruecolor(100, 40) or die('cannot create canvas.<br>'); $red = imagecolorallocate($image, 255, 0, 0); $green = imagecolorallocate($image, 0, 255, 0); $blue = imagecolorallocate($image, 0, 0, 255); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $white); imagerectangle($image, 1, 1, 99, 39, $black); $color = [$red, $green, $blue]; for ($i = 1; $i <= 100; $i++) { imagesetpixel($image, mt_rand(2, 98), mt_rand(2, 38), $color[mt_rand(0, 2)]); } $source = "abcdefghigklmnopqrstuvwxyz0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"; $first = $source[mt_rand(0, 61)]; $second = $source[mt_rand(0, 61)]; $third = $source[mt_rand(0, 61)]; $fourth = $source[mt_rand(0, 61)]; $_SESSION['captcha'] = $first . $second . $third . $fourth; $font = dirname(dirname(__DIR__)) . '/../public/assets/fonts/SpicyRice.ttf'; imagettftext($image, 20, mt_rand(-20, 20), 10, 30, $blue, $font, $first); imagettftext($image, 20, mt_rand(-20, 20), 30, 30, $blue, $font, $second); imagettftext($image, 20, mt_rand(-20, 20), 50, 30, $blue, $font, $third); imagettftext($image, 20, mt_rand(-20, 20), 70, 30, $blue, $font, $fourth); return imagejpeg($image); }
function mkthumb($img_src, $img_width = "100", $img_height = "100", $folder_scr = "include/files", $des_src = "include/files") { // Größe und Typ ermitteln list($src_width, $src_height, $src_typ) = getimagesize($folder_scr . "/" . $img_src); if (!$src_typ) { return false; } // calculate new size if ($src_width >= $src_height) { $new_image_height = $src_height / $src_width * $img_width; $new_image_width = $img_width; if ($new_image_height > $img_height) { $new_image_width = $new_image_width / $new_image_height * $img_height; $new_image_height = $img_height; } } else { $new_image_width = $src_width / $src_height * $img_height; $new_image_height = $img_height; if ($new_image_width > $img_width) { $new_image_height = $new_image_height / $new_image_width * $img_width; $new_image_width = $img_width; } } // for the case that the thumbnail would be bigger then the original picture if ($new_image_height > $src_height) { $new_image_width = $new_image_width * $src_height / $new_image_height; $new_image_height = $src_height; } if ($new_image_width > $src_width) { $new_image_height = $new_image_height * $new_image_width / $src_width; $new_image_width = $src_width; } if ($src_typ == 1) { $image = imagecreatefromgif($folder_scr . "/" . $img_src); $new_image = imagecreate($new_image_width, $new_image_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $src_width, $src_height); imagegif($new_image, $des_src . "/" . $img_src . "_thumb", 100); imagedestroy($image); imagedestroy($new_image); return true; } elseif ($src_typ == 2) { $image = imagecreatefromjpeg($folder_scr . "/" . $img_src); $new_image = imagecreatetruecolor($new_image_width, $new_image_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $src_width, $src_height); imagejpeg($new_image, $des_src . "/" . $img_src . "_thumb", 100); imagedestroy($image); imagedestroy($new_image); return true; } elseif ($src_typ == 3) { $image = imagecreatefrompng($folder_scr . "/" . $img_src); $new_image = imagecreatetruecolor($new_image_width, $new_image_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $src_width, $src_height); imagepng($new_image, $des_src . "/" . $img_src . "_thumb"); imagedestroy($image); imagedestroy($new_image); return true; } else { return false; } }
public function index() { $code = substr(sha1(mt_rand()), 17, 6); $this->session->set_userdata('captcha_code', $code); $width = '120'; $height = '40'; $font = APPPATH . 'modules/contact/assets/fonts/monofont.ttf'; $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); }
function UploadImage($bukti_name) { //direktori gambar $vdir_upload = "bukti/"; $vfile_upload = $vdir_upload . $bukti_name; //Simpan gambar dalam ukuran sebenarnya move_uploaded_file($_FILES["bukti"]["tmp_name"], $vfile_upload); //identitas file asli $im_src = imagecreatefromjpeg($vfile_upload); $src_width = imageSX($im_src); $src_height = imageSY($im_src); //Simpan dalam versi small 110 pixel //Set ukuran gambar hasil perubahan $dst_width = 50; $dst_height = $dst_width / $src_width * $src_height; //proses perubahan ukuran $im = imagecreatetruecolor($dst_width, $dst_height); imagecopyresampled($im, $im_src, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); //Simpan gambar imagejpeg($im, $vdir_upload . "small_" . $bukti_name); //Simpan dalam versi medium 360 pixel //Set ukuran gambar hasil perubahan $dst_width2 = 270; $dst_height2 = $dst_width2 / $src_width * $src_height; //proses perubahan ukuran $im2 = imagecreatetruecolor($dst_width2, $dst_height2); imagecopyresampled($im2, $im_src, 0, 0, 0, 0, $dst_width2, $dst_height2, $src_width, $src_height); //Simpan gambar imagejpeg($im2, $vdir_upload . "medium_" . $bukti_name); //Hapus gambar di memori komputer imagedestroy($im_src); imagedestroy($im); imagedestroy($im2); }
private static function __image() { self::__background(); self::__adulterate(); self::__font(); header("Expires: 0" . PHP_EOL); header("Cache-Control: no-cache" . PHP_EOL); header("Pragma: no-cache" . PHP_EOL); if (function_exists('imagejpeg')) { header('Content-type:image/jpeg' . PHP_EOL); $void = imagejpeg(self::$im); } else { if (function_exists('imagepng')) { header('Content-type:image/png' . PHP_EOL); $void = imagepng(self::$im); } else { if (function_exists('imagegif')) { header('Content-type:image/gif' . PHP_EOL); $void = imagegif(self::$im); } else { return false; } } } imagedestroy(self::$im); return $void; }
static function resizewidth($width, $imageold, $imagenew) { $image_info = getimagesize($imageold); $image_type = $image_info[2]; if ($image_type == IMAGETYPE_JPEG) { $image = imagecreatefromjpeg($imageold); } elseif ($this->image_type == IMAGETYPE_GIF) { $image = imagecreatefromgif($imageold); } elseif ($this->image_type == IMAGETYPE_PNG) { $image = imagecreatefrompng($imageold); } $ratio = imagesy($image) / imagesx($image); $height = $width * $ratio; //$width = imagesx($image) * $width/100; // $height = imagesx($image) * $width/100; $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $image_info[0], $image_info[1]); $image = $new_image; $compression = 75; $permissions = null; if ($image_type == IMAGETYPE_JPEG) { imagejpeg($image, $imagenew, $compression); } elseif ($image_type == IMAGETYPE_GIF) { imagegif($image, $imagenew); } elseif ($image_type == IMAGETYPE_PNG) { imagepng($image, $imagenew); } if ($permissions != null) { chmod($imagenew, $permissions); } }
function hacknrollify($picfilename) { $logofilename = "logo.png"; $logoPicPath = "logopics/" . $logofilename; $originalPicPath = "originalpics/" . $picfilename; $editedfilename = "hnr_" . $picfilename; $editedPicPath = "editedpics/" . $editedfilename; // read the original image from file $profilepic = imagecreatefromjpeg($originalPicPath); $profilepicWidth = imagesx($profilepic); $profilepicHeight = imagesy($profilepic); // create the black image overlay $blackoverlay = imagecreate($profilepicWidth, $profilepicHeight); imagecolorallocate($blackoverlay, 0, 0, 0); // then merge the black and profilepic imagecopymerge($profilepic, $blackoverlay, 0, 0, 0, 0, $profilepicWidth, $profilepicHeight, 50); imagedestroy($blackoverlay); // merge the resized logo $logo = resizeImage($logoPicPath, $profilepicWidth - 80, 999999); imageAlphaBlending($logo, false); imageSaveAlpha($logo, true); $logoWidth = imagesx($logo); $logoHeight = imagesy($logo); $verticalOffset = $profilepicHeight / 2 - $logoHeight / 2; $horizontalOffset = 40; imagecopyresampled($profilepic, $logo, $horizontalOffset, $verticalOffset, 0, 0, $logoWidth, $logoHeight, $logoWidth, $logoHeight); $mergeSuccess = imagejpeg($profilepic, $editedPicPath); if (!$mergeSuccess) { echo "Image merge failed!"; } imagedestroy($profilepic); imagedestroy($logo); return $editedPicPath; }
function createthumb($originalImage, $new_w, $new_h) { $src_img = imagecreatefromjpeg("uploads/" . $originalImage); # Add the _t to our image name list($imageName, $extension) = explode(".", $originalImage); $newName = $imageName . "_t." . $extension; # Maintain proportions $old_x = imageSX($src_img); $old_y = imageSY($src_img); if ($old_x > $old_y) { $thumb_w = $new_w; $thumb_h = $old_y * ($new_h / $old_x); } if ($old_x < $old_y) { $thumb_w = $old_x * ($new_w / $old_y); $thumb_h = $new_h; } if ($old_x == $old_y) { $thumb_w = $new_w; $thumb_h = $new_h; } # Create destination-image-resource $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); # Copy source-image-resource to destination-image-resource imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); # Create the final image from the destination-image-resource imagejpeg($dst_img, "uploads/" . $newName); # Delete our image-resources imagedestroy($dst_img); imagedestroy($src_img); # Show results return $newName; }
function resizer($image) { $name = md5(sha1(date('d-m-y H:i:s') . $image['tmp_name'])); $type = $image['type']; switch ($type) { case "image/jpeg": $img = imagecreatefromjpeg($image['tmp_name']); break; case "image/png": $img = imagecreatefrompng($image['tmp_name']); break; } $x = imagesx($img); $y = imagesy($img); $height = 1200 * $y / $x; $new = imagecreatetruecolor(1200, $height); imagecopyresampled($new, $img, 0, 0, 0, 0, 1200, $height, $x, $y); switch ($type) { case "image/jpeg": $local = "../assets/img/Portfolioimg/{$name}" . ".jpg"; imagejpeg($new, $local); break; case "image/png": $local = "../assets/img/Portfolioimg/{$name}" . ".png"; imagejpeg($new, $local); break; } return $local; }