2014年5月

[Laravel4] 创建一个 占位图片 服务

演示图片地址:http://usr.im/100x100

使用Composer安装intervention/image库

composer require intervention/image:dev-master

编码

这里并未对生成的图片进程缓存处理,需要的可以自行加上。

// vim app/routes.php
<?php
Route::pattern('width', '\d+');
Route::pattern('height', '\d+');
Route::get('{width}x{height}', 'ImageHolderController@getIndex');
// vim app/controllers/ImageHolderController.php
<?php

class ImageHolderController extends BaseController {
    public function getIndex($width, $height)
    {
        $width = intval($width);
        $height = intval($height);
        if ($width > 1900 || $height > 900)
            App::abort(404);
        $fontSize = min(max(intval($width / 5), 12), 38);
        $image = Image::canvas($width, $height, '#CCCCCC')
                ->line('#B5B5B5', 0, 0, $width, $height)
                ->line('#B5B5B5', $width, 0, 0, $height)
                ->text($width . 'x' . $height, $width / 2, $height / 2, function ($font) use ($fontSize) {
                    $font->file(public_path('font/Georgia.ttf'));
                    $font->align('center');
                    $font->valign('middle');
                    $font->size($fontSize);
                    $font->color('#666666');
                });
        // 2014-07-19 17:46 修复图片格式不正确的问题,->encode('png')
        return Response::make($image->encode('png'), 200, array('Content-Type' => 'image/png'));
    }
}

使用命令行创建dmg

自动获取软件版本号

APP_NAME="Soulver"
VERSION=$(/usr/libexec/plistbuddy -c Print:CFBundleShortVersionString: "${APP_NAME}.app/Contents/Info.plist")
DMG_BACKGROUND_IMG="Background.png"

VOL_NAME="${APP_NAME} ${VERSION}"
DMG_TMP="${VOL_NAME}-temp.dmg"
DMG_FINAL="${VOL_NAME}.dmg"
STAGING_DIR="./Install"

创建dmg

# 清理文件夹
rm -rf "${STAGING_DIR}" "${DMG_TMP}" "${DMG_FINAL}"
# 创建文件夹,拷贝,计算
mkdir -p "${STAGING_DIR}"
cp -rpf "${APP_NAME}.app" "${STAGING_DIR}"
SIZE=`du -sh "${STAGING_DIR}" | sed 's/\([0-9\.]*\)M\(.*\)/\1/'` 
SIZE=`echo "${SIZE} + 1.0" | bc | awk '{print int($1+0.5)}'`
# 容错处理
if [ $? -ne 0 ]; then
   echo "Error: Cannot compute size of staging dir"
   exit
fi
# 创建临时dmg文件
hdiutil create -srcfolder "${STAGING_DIR}" -volname "${VOL_NAME}" -fs HFS+ \
      -fsargs "-c c=64,a=16,e=16" -format UDRW -size ${SIZE}M "${DMG_TMP}"
echo "Created DMG: ${DMG_TMP}"

设置dmg

DEVICE=$(hdiutil attach -readwrite -noverify "${DMG_TMP}" | \
         egrep '^/dev/' | sed 1q | awk '{print $1}')
sleep 2
# 增加Applications目录的软链接
echo "Add link to /Applications"
pushd /Volumes/"${VOL_NAME}"
ln -s /Applications
popd
# 拷贝背景图片
mkdir /Volumes/"${VOL_NAME}"/.background
cp "${DMG_BACKGROUND_IMG}" /Volumes/"${VOL_NAME}"/.background/

# 使用applescript设置一系列的窗口属性
echo '
   tell application "Finder"
     tell disk "'${VOL_NAME}'"
           open
           set current view of container window to icon view
           set toolbar visible of container window to false
           set statusbar visible of container window to false
           set the bounds of container window to {400, 100, 938, 432}
           set viewOptions to the icon view options of container window
           set arrangement of viewOptions to not arranged
           set icon size of viewOptions to 72
           set background picture of viewOptions to file ".background:'${DMG_BACKGROUND_IMG}'"
           set position of item "'${APP_NAME}'.app" of container window to {160, 195}
           set position of item "Applications" of container window to {360, 195}
           close
           open
           update without registering applications
           delay 2
     end tell
   end tell
' | osascript

sync
# 卸载
hdiutil detach "${DEVICE}"

压缩dmg

echo "Creating compressed image"
hdiutil convert "${DMG_TMP}" -format UDZO -imagekey zlib-level=9 -o "${DMG_FINAL}"

# 清理文件夹
rm -rf "${DMG_TMP}"
rm -rf "${STAGING_DIR}"

echo 'Done.'
exit

PHP unpack VS Python unpack

unpack 对二进制数据解包。

php unpack的结果 数组的索引是从 1 开始的
python unpack的结果是 元祖,索引从 0 开始

php unpack可以传大于需要解包长度的二进制串
python unpack只能传入需要的长度的二进制串

php unpack 和 python unpack 的解包格式不一样,例如:

// php
unpack('V6', $bin);

# python
import struct
struct.unpack('<6L', bin[0:24])