Skip to content
Snippets Groups Projects
Commit facd4cbe authored by Bart Visscher's avatar Bart Visscher
Browse files

Contacts: Cleanup photo and thumbnail code

parent 3eff161b
Branches
No related tags found
No related merge requests found
<?php
/**
* ownCloud - Addressbook
*
* @author Jakob Sack
* @copyright 2011 Jakob Sack mail@jakobsack.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
* Copyright (c) 2011, 2012 Bart Visscher <bartv@thisnet.nl>
* Copyright (c) 2011 Jakob Sack mail@jakobsack.de
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
// Init owncloud
......@@ -29,67 +17,36 @@ $id = $_GET['id'];
if(isset($GET['refresh'])) {
header("Cache-Control: no-cache, no-store, must-revalidate");
}
$l10n = new OC_L10N('contacts');
$content = OC_Contacts_App::getContactVCard($id);
$contact = OC_Contacts_App::getContactVCard($id);
$image = new OC_Image();
// invalid vcard
if( is_null($content)){
$image->loadFromFile('img/person_large.png');
header('Content-Type: '.$image->mimeType());
$image();
//echo $l10n->t('This card is not RFC compatible.');
exit();
if( is_null($contact)) {
OC_Log::write('contacts','photo.php. The VCard for ID '.$id.' is not RFC compatible',OC_Log::ERROR);
} else {
// Photo :-)
foreach($content->children as $child){
if($child->name == 'PHOTO'){
$mime = 'image/jpeg';
foreach($child->parameters as $parameter){
if( $parameter->name == 'TYPE' ){
$mime = $parameter->value;
}
}
if($image->loadFromBase64($child->value)) {
if($image->width() > 200 || $image->height() > 200) {
$image->resize(200);
}
header('Content-Type: '.$mime);
$image();
exit();
} else {
$image->loadFromFile('img/person_large.png');
header('Content-Type: '.$image->mimeType());
$image();
}
//$photo = base64_decode($child->value);
//header('Content-Type: '.$mime);
//header('Content-Length: ' . strlen($photo));
//echo $photo;
//exit();
if($image->loadFromBase64($contact->getAsString('PHOTO'))) {
// OK
header('ETag: '.md5($contact->getAsString('PHOTO')));
}
}
}
$image->loadFromFile('img/person_large.png');
header('Content-Type: '.$image->mimeType());
$image();
/*
else
// Logo :-/
foreach($content->children as $child){
if($child->name == 'PHOTO'){
$mime = 'image/jpeg';
foreach($child->parameters as $parameter){
if($parameter->name == 'TYPE'){
$mime = $parameter->value;
if($image->loadFromBase64($contact->getAsString('LOGO'))) {
// OK
header('ETag: '.md5($contact->getAsString('LOGO')));
}
if ($image->valid()) {
$max_size = 200;
if($image->width() > $max_size ||
$image->height() > $max_size) {
$image->resize($max_size);
}
$photo = base64_decode($child->value());
header('Content-Type: '.$mime);
header('Content-Length: ' . strlen($photo));
echo $photo;
exit();
}
}
*/
if (!$image->valid()) {
// Not found :-(
//echo $l10n->t('This card does not contain a photo.');
$image->loadFromFile('img/person_large.png');
}
header('Content-Type: '.$image->mimeType());
$image->show();
//echo OC_Contacts_App::$l10n->t('This card does not contain a photo.');
......@@ -29,15 +29,11 @@ OC_Util::checkAppEnabled('contacts');
function getStandardImage(){
$date = new DateTime('now');
$date->add(new DateInterval('P10D'));
header('Expires: '.$date->format(DateTime::RFC850));
header('Expires: '.$date->format(DateTime::RFC2822));
header('Cache-Control: cache');
header('Pragma: cache');
header("HTTP/1.1 307 Temporary Redirect");
header('Location: '.OC_Helper::imagePath('contacts', 'person.png'));
exit();
// $src_img = imagecreatefrompng('img/person.png');
// header('Content-Type: image/png');
// imagepng($src_img);
// imagedestroy($src_img);
}
if(!function_exists('imagecreatefromjpeg')) {
......@@ -50,10 +46,10 @@ $id = $_GET['id'];
$l10n = new OC_L10N('contacts');
$content = OC_Contacts_App::getContactVCard($id);
$contact = OC_Contacts_App::getContactVCard($id);
// invalid vcard
if( is_null($content)){
if( is_null($contact)){
OC_Log::write('contacts','thumbnail.php. The VCard for ID '.$id.' is not RFC compatible',OC_Log::ERROR);
getStandardImage();
exit();
......@@ -62,34 +58,45 @@ if( is_null($content)){
$thumbnail_size = 23;
// Find the photo from VCard.
foreach($content->children as $child){
if($child->name == 'PHOTO'){
$image = new OC_Image();
if($image->loadFromBase64($child->value)) {
$photo = $contact->getAsString('PHOTO');
$etag = md5($photo);
$rev_string = $contact->getAsString('REV');
if ($rev_string) {
$rev = DateTime::createFromFormat(DateTime::W3C, $rev_string);
$last_modified_time = $rev->format(DateTime::RFC2822);
} else {
$last_modified_time = null;
}
header('Cache-Control: cache');
header('Pragma: cache');
if ($rev_string) {
header('Last-Modified: '.$last_modified_time);
}
header('ETag: '.$etag);
if (@trim($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||
@trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
header('HTTP/1.1 304 Not Modified');
exit;
}
if($image->loadFromBase64($photo)) {
if($image->centerCrop()) {
if($image->resize($thumbnail_size)) {
header('ETag: '.md5($child->value));
if(!$image()) {
OC_Log::write('contacts','thumbnail.php. Couldn\'t display thumbnail for ID '.$id,OC_Log::ERROR);
getStandardImage();
if($image->show()) {
exit();
} else {
OC_Log::write('contacts','thumbnail.php. Couldn\'t display thumbnail for ID '.$id,OC_Log::ERROR);
}
} else {
OC_Log::write('contacts','thumbnail.php. Couldn\'t resize thumbnail for ID '.$id,OC_Log::ERROR);
getStandardImage();
exit();
}
}else{
OC_Log::write('contacts','thumbnail.php. Couldn\'t crop thumbnail for ID '.$id,OC_Log::ERROR);
getStandardImage();
exit();
}
} else {
OC_Log::write('contacts','thumbnail.php. Couldn\'t load image string for ID '.$id,OC_Log::ERROR);
getStandardImage();
exit();
}
exit();
}
}
getStandardImage();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment